Help With Detector Script

Seems this isn’t working some reason. Can I get some help?

script.Parent.Touched:Connect(function(hit)
		if hit.Name == "SnacksCrate" then
			hit:Destroy()
			local plr = hit.Parent.Parent.Name
			local player = game.Players:FindFirstChild(plr)
			player.Cash.Value = player.Cash.Value +10
			script.Parent.Parent.CandyLeft.Value = script.Parent.Parent.CandyLeft.Value +1
	end
end)
2 Likes

is the crate one part or a model made with multiple parts?

I believe this should fix your code. I used += operators and destroyed the hit at the end so you’re not referencing a nil value.

script.Parent.Touched:Connect(function(hit)
    if hit.Name == "SnacksCrate" then
		local plr = hit.Parent.Parent.Name -- ????
		local player = game.Players:FindFirstChild(plr)
		player.Cash.Value += 10
		script.Parent.Parent.CandyLeft.Value += 1
		hit:Destroy()
	end
end)

Looks like you aren’t getting the player character correctly. Try this…

script.Parent.Touched:Connect(function(hit)
	if hit.Name == "SnacksCrate" then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not player then return end

		hit:Destroy()
		player.Cash.Value += 10
		script.Parent.Parent.CandyLeft.Value += 1
	end
end)

The function you’re looking for is :GetPlayerFromCharacter(hit.Parent) instead of :FindFirstChild(Player), you can then access the player’s leaderstats.

Example:

local Players = game:GetService('Players')


local function Touched
(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	if (Player)
	then
		local Leaderstats = Player:FindFirstChild('leaderstats')
        -- // Assuming you're storing it inside leaderstats.
		if (Leaderstats)
		then
			local Cash = Leaderstats:FindFirstChild('Cash')
			if (Cash)
			then
				Cash.Value += 10 -- Same as Cash.Value = Cash.Value + 10.
			end
		end
	end
end


script.Parent.Touched:Connect(Touched)

I’m not too sure as to why you want to destroy the hit object, however you’re free to add that yourself.
I would also recommend adding a debounce, unless you want it this way.

It would probably be helpful to see the error message (or description of why it doesn’t work).

Some things that might go wrong:
You destroy the hit object. That sets the parent to nil. Then you look for the parent of the hit part…

You assume that the parent of the parent of the hit part must be the character model. That might be true, but it’s better to check if you found anything. It’s better to get the player from the character by using Players:GetPlayerFromCharacter(char) instead of getting the name and using FindFirstChild. This function also returns nil when no player is found (because it wasn’t actually a character).

But to be clear, let’s not confuse things. Is this script in a part in workspace, and is the ‘hit’ part of the players character when he touches it? Or is this script in the player character, and is the hit supposed to be some part in workspace named ‘SnacksCrate’? The latter won’t work… Therefore:

The current script seems to assume being inside a part in workspace, and looking for a part named ‘SnacksCrate’ which should be part of the player’s character model. Did you put such a part in there, in such a way that the parent’s parent of that snackcrate is the character model? I think this is probably where it went wrong, by mixing up the hit and the touchdetecting part.

Good luck!