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

Why when i touching the block it gives me 20-30 leaderstats value when i need 1?

Asked by 2 years ago
Edited 2 years ago

I made script what counts on what stage you are. So i scripted when you touching portal leaderstats value needs to be 1, it gives me around 25 stages?

Leaderstats script : ~~~~~~~~~~~~~~~~~

local function onPlayerJoin(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player


    local money = Instance.new("IntValue")
    money.Name = "Stages"
    money.Value = 0
    money.Parent = leaderstats
end

game.Players.PlayerAdded:Connect(onPlayerJoin)

local touchName = "Stages"
local rs = game:GetService("ReplicatedStorage")

local touchEvent = rs:WaitForChild('TouchEvent')


touchEvent.OnServerEvent:Connect(function(plr)

    plr.leaderstats[touchName].Value = plr.leaderstats[touchName].Value +1

    wait(1)

end)


StarterPack script: ~~~~~~~~~~~~~~~~~ local touchName = "Stages" local rs = game:GetService("ReplicatedStorage") local touchEvent = rs:WaitForChild('TouchEvent') local portal = workspace.FirstStage.FrostPartal.PortalPart portal.Touched:Connect(function() touchEvent:FireServer() end)

1 answer

Log in to vote
1
Answered by
TGazza 1336 Moderation Voter
2 years ago

The problem is you have no touch "Debounce" in your portal. So it looks like it's firing each time a part of the player like arms, legs, accessories etc... touch the portal part.

To fix that, change this:

StarterPack script:
 ~~~~~~~~~~~~~~~~~
local touchName = "Stages"
local rs = game:GetService("ReplicatedStorage")

local touchEvent = rs:WaitForChild('TouchEvent')
local portal = workspace.FirstStage.FrostPartal.PortalPart

portal.Touched:Connect(function()
    touchEvent:FireServer()
end)

To this:

StarterPack script:
 ~~~~~~~~~~~~~~~~~
local touchName = "Stages"
local rs = game:GetService("ReplicatedStorage")

local touchEvent = rs:WaitForChild('TouchEvent')
local portal = workspace.FirstStage.FrostPartal.PortalPart

--// debounce code! Variable declare
local Debounce = false

portal.Touched:Connect(function()
    if(Debounce == false) then --// check for our debounce is false or accepting 
        Debounce = true --// set it to true for this part
        touchEvent:FireServer() --// call the server script
        wait(1) --// wait 1 second to stop repeated touches, change this to your liking but not too small otherwise it will bug out again!.
        Debounce = false --// free up our debounce variable for next run
    end
end)

Hope this helps! :)

0
Thanks so much! I have one question, how to make script work once for one player? Because i have button "Back To Start" SashaPro336 47 — 2y
Ad

Answer this question