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

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

Asked by
Gigaset39 111
2 years ago
Edited 2 years ago

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

Log in to vote
1
Answered by
imKirda 4491 Moderation Voter Community Moderator
2 years ago
Edited 2 years ago

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

0
Interesting,thanks. Gigaset39 111 — 2y
0
That joke whit Elon Musk :D Gigaset39 111 — 2y
Ad

Answer this question