Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

if more players touch the MoneyGiver part, just one gets the money,why?

Hello world, in my game there is a part called MoneyGiver, and when a player touches it, he gets money, but if 2 players touch the part at the same time, just one of them gets the money, why? also, Sabailurize (A Helper form this community) changed this script to make it work, thanks Sabailurize.

But now im trying to make it work soo, even when 2 or more players touches it, they both get that money: (im not sure, but ”Touches” is not a word, but you get it)

 local waittime = 10
 local isTouched = false 

 script.Parent.Touched:Connect(function(hit)
     if hit and hit.Parent:FindFirstChild("Humanoid") and not isTouched then
	     isTouched = true
	     local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	     wait(1) 
	     player.leaderstats.Money.Value += 10
	     wait(waittime)
	     isTouched = false

   end
     end)

1 answer

✓ Accepted answer

your script has something called debounce, this is made so you don't spam touch and become elon mask, instead you must wait 1 second after each touch to touch again, in your case this debounce is equal to everybody, 1 player touches it and all others have to wait 1 second too. solution is to make each player have individual debounce, you can use attributes for that.

local waittime = 10

script.Parent.Touched:Connect(function(hit)
    if hit and hit.Parent:FindFirstChild("Humanoid") and not hit.Parent:GetAttribute("isTouched") then
        hit.Parent:SetAttribute("isTouched", true)
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        wait(1)
        player.leaderstats.Money.Value += 10
        wait(waittime)
        hit.Parent:SetAttribute("isTouched", false)
    end
end)

here are brief explainations:

not hit.Parent:GetAttribute("isTouched")

^^^ checks if there is no such attribute isTouched

hit.Parent:SetAttribute("isTouched", true)

^^^ creats a new attribute isTouched with value true

hit.Parent:SetAttribute("isTouched", false)

^^^ sets value of isTouched attribute to false which is almost same as if the attribute does not exist in this case

2 comments

0

Interesting,thanks.

0

That joke whit Elon Musk :D

This discussion is preserved as it was. New answers, comments, and votes are closed.