Coin Script not working as intended

What Is The Error
No Error

What Are You Working On
A Coin For My Simulator.

What Script Is It In
Well Sometimes LocalScripts are the problem for everything But this time No Lol.

The Code

local Coin = script.Parent

Coin.Touched:Connect(function(Hit)
	local Humanoid = Hit.Parent:FindFirstChildWhichIsA('Humanoid')
	if Humanoid then
		game.Players.LocalPlayer.leaderstats.Value = game.Players.LocalPlayer.leaderstats.Value+1
		Coin.Transparency = 1
		wait(60)
		Coin.Transparency = 0
	end
end)
1 Like

Try this!

local Coin = script.Parent
local d = false
Coin.Touched:Connect(function(Hit)
	if d then return end
	d = true
	local Humanoid = Hit.Parent:FindFirstChildWhichIsA('Humanoid')
	if Humanoid then
		game.Players.LocalPlayer.leaderstats.Value += 1
		Coin.Transparency = 1
		wait(60)
		Coin.Transparency = 0
	end
	d = false
end)

It uses a scripting concept called “debounce” to make sure the .Touched event’s function isn’t being used more than once at a time.

Im, Not sure if it’ll, Change anything since its an Debounce. But I’ll atleast try.

Code, is still not giving an error and not giving coins.

Is the problem that when you step on it it doesnt give you the money

It doesnt give me “Coins” thats how the leaderstats is typed with capitol “C” in Coins.

And, Yes thats the problem. its not giving me anything.

Oops, I made a mistake!

local Coin = script.Parent
local d = false
Coin.Touched:Connect(function(Hit)
	if d then return end
	d = true
	local Humanoid = Hit.Parent:FindFirstChildWhichIsA('Humanoid')
	if Humanoid then
		game.Players.LocalPlayer.leaderstats.Coins.Value += 1
		Coin.Transparency = 1
		wait(60)
		Coin.Transparency = 0
	end
	d = false
end)

Okay let me try, this to see if works.

Still, Doesnt work without an error should i put it in a normal script or keep it in Local Script.

Another issue might be the fact that this code is client sided, your best bet is to turn the code into serversided code, this is how you’d do so:

local Coin = script.Parent
local d = false
Coin.Touched:Connect(function(Hit)
	if d then return end
	d = true
	local Humanoid = Hit.Parent:FindFirstChildWhichIsA('Humanoid')
	if Humanoid then
		local Plr = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if Plr then
			Plr.leaderstats.Coins.Value += 1
			Coin.Transparency = 1
			wait(60)
			Coin.Transparency = 0
		end
	end
	d = false
end)

Put this in a Script instead!

I think the problem is that you wrote FindFirstChildWhichIsA(“Humanoid”) instead you need to write FindFirstChild(“Humanoid”)

I don’t think so, I think both work!

Yeah, I think both will work, Whats the difference im just getting the name of the child.

  1. LocalPlayer can’t be used inside serverscripts.
  2. You’ll need to add a debounce so the player doesn’t keep getting coins even if it’s invisible.
  3. Your trying to increase a folder’s value, folder’s don’t have a value so the player’s coins wont be increased and would error.
local Coin = script.Parent
local Touched = false

Coin.Touched:Connect(function(Hit)
    if Touched then
        return
    end

	local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)

	if Player then
        Touched = true
		Player.leaderstats.Coins.Value += 1
		Coin.Transparency = 1
		wait(60)
		Coin.Transparency = 0
        Touched = false
	end
end)

The script is not in serverscript, its in the Coin/Part.

If it’s a LocalScript this wouldn’t work since then the coin change would only replicate to the player’s screen and LocalScripts only work in places which are a descendant of a Player.

Okay, ill try in a normal script.

Still, does not work evan if its in Script or Local Script

Maybe try using a remote function? It might not fix it but it will remove the error (I think).