I need help with my Script

Hello There,

I’m having issues With my script It seem to not Add cash to the leaderstats, I’ve tried getting the Service from SrciptService but it don’t nothing.

If someone Could help Me It Would be Really Helpful :slight_smile:


Normal Script in the Part

local Player = game.Players.LocalPlayer
local leaderstats = game:GetService("ServerScriptService")

script.Parent.Touched:connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	if Humanoid then
		Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 1500
	else
		warn("Error")
	end
end)


Leaderstats Script

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats= Instance.new("Folder")
	leaderstats.Name= ("leaderstats")
	leaderstats.Parent= player


	local Cash= Instance.new("IntValue")
	Cash.Name= ("Cash")
	Cash.Parent = leaderstats
Cash.Value = 0

end)

Official_Simulation

You can’t get local player from normal script so to get the player inside if statement do this:

local player = Players:FindFirstChild(hit.Parent.Name)

and change the player variable to just game.Players

1 Like

It Worked But How Would I get it to Do it once?

Because It’s Going by When the player moves. It Keeps Adding until the player Is not On the Part.

Make a debounce or connect it with :Once

1 Like

I did this for a debounce But nothing Happened

local leaderstats = game:GetService("ServerScriptService")
debounce = false

script.Parent.Touched:connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	if Humanoid and debounce == false then
debounce = true
local Player = Players:FindFirstChild(hit.Parent.Name)
		Player.leaderstats.Cash.Value = Player.leaderstats.Cash.Value + 1500
	else
		warn("Error")
	end
debounce = false
end)

Do need a variable For the Debounce or no?

try this:

local debounce = false

script.Parent.Touched:connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid")
	if Humanoid and debounce == false then
		debounce = true
		local Player = Players:FindFirstChild(hit.Parent.Name)
		Player.leaderstats.Cash.Value += 1500
		task.wait(how mouch time you want to wait )
		debounce = false
	end
end)
1 Like

It Did nothing Sorry, Should I Try a New method?

Since this is inside of a normal script, it needs to be a local script and then you’ll need to make a change to the normal script that can give the cash through a remote event inside of ReplicatedStorage. Try something such as this;

Local Script inside of StarterPlayerScripts

game.Workspace.PartNameHere.Touched:Connect(function()
	local Key = "KeyHere"
	game.ReplicatedStorage.RemoteEvent:FireServer(Key)
	script.Disabled = true
end)

Normal Script Inside of ServerScriptService

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats= Instance.new("Folder")
	leaderstats.Name= ("leaderstats")
	leaderstats.Parent= player


	local Cash= Instance.new("IntValue")
	Cash.Name= ("Cash")
	Cash.Parent = leaderstats
	Cash.Value = 0
	
	local GivenFreeCash = Instance.new("BoolValue",player) GivenFreeCash.Name = "GivenFreeCash"
end)

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr,key)
	if key == "KeyHere" and plr.GivenFreeCash == false then
		plr.leaderstats.Cash.Value += 1500
		plr.GivenFreeCash = true
	end
end)

This should work if I am correct, also change “KeyHere” to whatever you want the key to be, and change “PartNameHere” to the name of the part. Let me know if anything is wrong.

So I need a Remote event for the Local Script?

Yes, put it inside of ReplicatedStorage.

1 Like

You can do the same thing with just a normal script

did you forget the Players varibale?

I didn’t Forget the Player Variable also @ThatTH3Guy For the Key Do I just put in the Part name?

For the key you want it to be something secret that nobody will know, and it has to be the same on both scripts.

1 Like

I think I Solve My Issue thank you guys :slight_smile: