I have this simple script where the player gets cash when they touch a particular part. It doesn’t seem to work properly.
game.Workspace.Tower:WaitForChild("Stage2"):WaitForChild("StartPlatform").Touched:Connect(function(hit)
local char = hit.Parent
local player = game.Players:GetPlayerFromCharacter(char)
local playerCash = player.leaderstats.Drops.Value
print("hi")
if not Floor1Won then
Floor1Won = true
playerCash += 5
end
end)
You are changing the playerCash variable by 5. Not the player cash’s value
To change the playerCash’s value, do:
game.Workspace.Tower:WaitForChild("Stage2"):WaitForChild("StartPlatform").Touched:Connect(function(hit)
local char = hit.Parent
local player = game.Players:GetPlayerFromCharacter(char)
local playerCash = player.leaderstats.Drops
print("hi")
if not Floor1Won then
Floor1Won = true
playerCash.Value += 5
end
end)
and you can use @TheRealANDRO’s code to make the code more optimized
But it doesn’t work, idk how to explain it so I will use an example:
Let’s say drops has a starting value of 0
local value = Drops.Value
print(value) -- Prints 0
print(Drops.Value) -- Prints 0
value += 5
print(value) -- Prints 5
print(Drops.Value) -- Prints 0
But if you set the value variable to Drops itself:
local value = Drops
print(value.Value) -- Prints 0
print(Drops.Value) -- Prints 0
value.Value+= 5
print(value.Value) -- Prints 5
print(Drops.Value) -- Prints 5
local Stage = game.Workspace.Tower:WaitForChild("Stage2") -- Waiting for parts
local Part = Stage:WaitForChild("StartPlatform")
Part.Touched:Connect(function(hit) -- Detecting when touched
local character = hit.Parent
if not character then return end -- Checking if hit.Parent exists
local player = game.Players:GetPlayerFromCharacter(character)
if not player then return end -- Checking if the found character actually belongs to a player
local playerCash = player.leaderstats.Drops
print("hi")
if not Floor1Won then
Floor1Won = true
playerCash.Value += 5 -- Increasing the value
end
end)
Make sure Floor1Won actually exists and is a boolean.
Make sure the parts load in correctly.
Then make sure the parts load correctly. (Add another print statement to the first line of the function to check if it runs.)
It might be because of that. Also, make sure the code passes if checks as well.
If the parts get destroyed, then the code might not run after 5 minutes.
Make sure to put the events in a loop.
Also did you add a print statement to the first line of the function as I said?