Hello, I’ve got an issue with a script. No idea whats causing the issue as everything seems fine and I’m not getting any weird output.
I have a script (below) which is supposed to give the player 1000 cash when they touch a part then it teleports them.
I’ve tried changing how the cash variable is declared, but it still sets the cash instead of adding 1000 which I’ve told the script to do.
Script:
local Pad = game.Workspace.Pad2
script.Parent.Touched:Connect(function(hit)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) if player then
local CurrentlyTeleporting = player.Character:FindFirstChild("CurrentlyT")
local Cash = player.leaderstats.Cash
if not CurrentlyTeleporting then return end
if not CurrentlyTeleporting.Value then
CurrentlyTeleporting.Value = true
Cash.Value = Cash.Value + 1000
player.Character.HumanoidRootPart.CFrame = Pad.CFrame + Vector3.new(0,5,0)
wait(3)
CurrentlyTeleporting.Value = false
end
end
end)
Sorry if the post is kind of rough, first time posting here.
You need to make sure the object has a humanoid. Try this -
local Pad = game.Workspace.Pad2
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
local CurrentlyTeleporting = player.Character:FindFirstChild("CurrentlyT")
local Cash = player.leaderstats.Cash
if not CurrentlyTeleporting then return end
if CurrentlyTeleporting.Value == false then
CurrentlyTeleporting.Value = true
Cash.Value = Cash.Value + 1000
player.Character.HumanoidRootPart.CFrame = Pad.CFrame + Vector3.new(0,5,0)
task.wait(3)
CurrentlyTeleporting.Value = false
end
end
end
end)
Thanks for the quick reply, I have the exact same thing happen with the script you supplied. It teleports the player, but sets the players value to 1000 instead of adding 1000.
Yup, the 10 being added every 5 seconds is controlled by a local script in starter player scripts. I can easily swap it to a server script, just made it a local script because it’s easier to get the player through one lol.
I’ll try swapping it to a server script here in a moment.
Yeah well that is the issue, the server still thinks you have 0 cash (since you only added it on the client) so 0+1000=1000
Which makes it look like it’s setting it
Just swapped to a server script and it’s Kind of working. It adds the 1000 but also a little extra, which I guess is better than resetting the players cash to 1000 lol.
Thanks for the help, and for the explanation on the issue appreciate it.