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

Why is my script removing ten when i told it to only remove five, how is that even possible?

server script there is nothing that is changing the value xept this i have checked many times my script:

local tool = game.ReplicatedStorage.Tools.Sign
local remote = game.ReplicatedStorage.Tools.Buy_Sign
print("runned")
--------------------------------------------------
remote.OnServerEvent:Connect(function(player,Vorth)
	------------------------------------------------
	local coins = player:WaitForChild("leaderstats").Coins
	-------------------------------------------------
	--remove coins
	coins.Value = coins.Value - 5
	
	--Move tool to the player and set id.
	tool.Parent = player.Backpack
end

8 comments

0

Your event can be firing twicee

0

It is using a text button

0

print (coins.Value - 5)

0

yea it must be running twice

0

It print's -5

0

It is Not running twice i know that

0

whats the result of printing

0

Thankyou everyone for trying to help i really apreciate it!

2 answers

✓ Accepted answer

Hello, Freddan2006YT!

I think the only thing missing on your script is a debounce variable, it prevents the code from running twice on a small time period

I've added it to your script

local tool = game.ReplicatedStorage.Tools.Sign
local remote = game.ReplicatedStorage.Tools.Buy_Sign

local deb = false --Variable used for debounce

print("runned")
--------------------------------------------------
remote.OnServerEvent:Connect(function(player,Vorth)
   if(deb == false)then -- Lets code execute if variable "deb" is false
      deb = true -- Prevents code from running again
      ------------------------------------------------
      local coins = player:WaitForChild("leaderstats").Coins
      -------------------------------------------------
      --remove coins
      coins.Value = coins.Value - 5

      --Move tool to the player and set id.
      tool.Parent = player.Backpack
      wait(0.5)--Wait 0.5 before making code able to run again
      deb = false
    end
end

Edit¹: Also, this prevents hackers/exploiters from flooding their money change(or others)

Edit²: It would be good if you add something to check if player money is less than 5, so the player would not be able to have negative money

If I helped, please upvote and mark answer as accepted

5 comments

0

It dosen't even remove any coins now or add the item

0

hmmm

0

I really don't know why, do it prints any errors on developer console(F9)?

0

Because deb is nil and will never equal false

1

@incapaxx I've just changed it, I hope it works now(thanks for the hint)

I have kinda fixed it I'm very confused why I need to do this to make it work someone explain if you know why

local tool = game.ReplicatedStorage.Tools.Sign
local remote = game.ReplicatedStorage.Tools.Buy_Sign

local deb --Variable used for debounce
--------------------------------------------------
remote.OnServerEvent:Connect(function(player)
   if not deb then  -- Lets code execute if variable "deb" is false
      deb = true -- Prevents code from running again
      ------------------------------------------------
      local coins = player:WaitForChild("leaderstats").Coins
      -------------------------------------------------
      --remove coins
      coins.Value = coins.Value - 5
      tool.Parent = player.Backpack
      wait(0.5)--Wait 0.5 before making code able to run again
      deb = false
      coins.Value = coins.Value +5
    end
end)

2 comments

0

I'm gonna accept you becuse tried to help me :)

0

You need to do because sometimes roblox glitches and fires a client/server 2x

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