我如何制作 2x 现金游戏通行证脚本 Roblox

How do i make 2x cash gamepass script Roblox

所以我正在制作一个每分钟给你 5 现金的脚本,我还为脚本制作了一个游戏通行证,如果有人拥有游戏通行证,他们将获得非游戏通行证持有人的双倍金钱。这是我的脚本 我没有任何脚本来提供现金,但问题出在第二个脚本块中,控制台打印错误:

  09:10:57.466 ServerScriptService.CashGiver:6: attempt to index nil with 'UserId' - Server - CashGiver:6

local Give5Cash = game.ReplicatedStorage:WaitForChild("Give5Cash")
local Give10Cash = game.ReplicatedStorage:WaitForChild("Give10Cash")

Give5Cash.OnServerEvent:Connect(function()
    print("Player Will Be Given 5 Cash")
end)

Give10Cash.OnServerEvent:Connect(function()
    print("Player Will Be Given 10 Cash")
end)
while wait() do
    local MPS = game:GetService("MarketplaceService")
    local id = 16031172
    local player = game.Players.LocalPlayer

    if MPS:UserOwnsGamePassAsync(player.UserId, id) then
        game.ReplicatedStorage:WaitForChild("Give10Cash"):FireServer()
        print("Player Owns 2x Cash")
    else
        print("Players Doesnt Owns 2x Cash")
        game.ReplicatedStorage:WaitForChild("Give5Cash"):FireServer()
    end 
    
    wait(5)
end
local player = Players.LocalPlayer
if MPS:UserOwnsGamePassAsync(player.UserId, id) then

...

这里你给 player 分配了一个 nil 值,你可能不会索引但会这样做。

来自Roblox manual

Players.LocalPlayer

NotReplicated

This item is not replicated across Roblox’s server/client boundary.

LocalPlayer is a read-only property which refers to the Player whose client is running the game.

This property is only defined for LocalScripts (and ModuleScripts required by them), as they run on the client. For the server (on which Script objects run their code), this property is nil.

所以我找到了解决这个问题的方法,我的问题得到了答案。 这是我的做法 我在 ServerScriptService 中制作了 3 脚本,名称为 CashGiver5CashGiver10CashGiverHandler 这是我添加到每个脚本的脚本。 CashGiver5:

while wait(1) do
    print("Giving Player 5 Cash ")
    for i, player in pairs(game.Players:GetPlayers()) do
        player:WaitForChild("leaderstats").Cash.Value += 5
    end
end

CashGiver10:

while wait(1) do
    print("Giving Player 10 Cash ")
    for i, player in pairs(game.Players:GetPlayers()) do
        player:WaitForChild("leaderstats").Cash.Value += 10
    end
end

CashGiverHandler:

local MarketPlace = game:GetService("MarketplaceService")

game.Players.PlayerAdded:Connect(function(player)
    local g = 16031172 -- DOUBLE CASH ID
    
    local Give5Script = game.ServerScriptService.CashGiver5
    local Give10Script = game.ServerScriptService.CashGiver10

    if MarketPlace:UserOwnsGamePassAsync(player.UserId, g) then
        Give5Script:Destroy()
    else
        Give10Script:Destroy()
    end
end)

脚本的作用是什么?

所以基本上,CashGiver 脚本是每秒给玩家 Cash 的基本脚本。 因此,当玩家被添加到游戏中时,处理程序脚本会销毁其中一个脚本。