Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Need help with cash give?

Asked by
Zyleak 70
9 years ago

I want this 1 script to make it so that whenever someone touches a part in a certain model, they get cash (so i do not need scripts in each part) This is what I have so far but it does not work.

Bananas = game.Workspace.Bananas:GetChildren()
Bananas.Touched:connect(function(hit)
local User = game.Players:GetPlayerFromCharacter(hit.Parent)
User.Data.Bananas.Value = User.Data.Bananas.Value + 1
end)

1 answer

Log in to vote
1
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 years ago

How do you always know that it's a player touching it? If you're going to use :GetChildren(), you need to use a for loop!

for i,v in pairs(game.Workspace.Bananas:GetChildren()) do -- The Generic For loop
    v.Touched:connect(function(hit) -- Touched event
        if hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) then -- Checks if it's a player
            local p = game.Players:GetPlayerFromCharacter(hit.Parent) -- Sets the player variable
            p.Data.Bananas.Value = p.Data.Bananas.Value + 1 -- Adds 1 to the value!
        end
    end)
end
Ad

Answer this question