简体   繁体   中英

My chest script won't add the gold to my stats

game.Players.PlayerAdded:Connect(function(player)
    script.Parent.Touched:Connect(function(hit)
        if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
            local players = game:GetService("Players")
            local clone = game.Workspace.Sparkles:Clone()
            clone.Parent = game.Workspace
            clone.CanCollide = false
            clone.Position = script.Parent.Position
            
            script.Parent:Destroy()
             
            wait(1)
            clone:Destroy()
            local player = game.players.LocalPlayer
            players.LocalPlayer.leaderstats.gold.value = 10
        end
    end)
end)

Here, I'm trying to make a script where if you touch a chest it will give you gold[my stat name] but for some reason, it wont run properly. it wont give me the amount of gold i told it to

When you say script.Parent:Destroy() thats going to delete the script's parent right? So it's going to destroy the script's parent's children which includes the script, which means it can't give you the gold.

Tip: If you ever encounter an error, it might help to put some print statements for logging. For example in your code:

game.Players.PlayerAdded:Connect(function(player) print("Player has joined!")

Just repeat this for every function, loop, if statement, etc.(with different print messages of course) and then test this and see what gets printed. Also put a print statement after you destroy the script's parent to check if the script stays or not.

From my understanding, you have a function that fires whenever a player joins the game. And then when the part is touched, the part then fires the code you have. I would remove the game.Players.PlayerAdded:Connect(function(player) function as when it get's hit it will fire for every single player who has joined.

But why am I not gaining any gold? If you are using a server sided script you can't just call game.Players.LocalPlayer as that is client sided. LocalPlayer is an object that is in client sided scripts that tell the client sided script(s) what player they are executing on.

How do I fix this? First, you already have a statement to decide whether the hit part is a character. So, you can do,

local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
Player.leaderstats.gold.Value += 10

I've fixed a few things in your scripts and removed some things as well. First, I added a += 10 to the gold adding script as if you don't it will just set the gold value to 10 and never add any more. LocalPlayer is not available to be used on server sided scripts and should only be used on Client sided. (For future references, in your first initial code you have code that sets the variable of player but under that line you call players . (however, that wouldn't still work. Just trying to provide information about code that's syntax was incorrect.))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM