Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I am in need of help, my script when a player touches a part, they get money is not working, why?

My script is this below, there are no errors it's just not working. It is supposed to be an obby reward thing, once the player touches it they are rewarded with some Money. It isn't working for some reason.

local Goal = script.Parent
local function onModelTouched(part)
local character = game.Workspace.Player
local player = game.Players:GetPlayerFromCharacter(character)
player.WaitForChild("leaderstats").Money = player.WaitForChild("leaderstats").Money + 1
Goal.touched:Connect(onModelTouched)
end

2 answers

✓ Accepted answer

:WaitForChild() is a method, which means it uses a colon to call it. .Touched() is an event, the T is capitalized.

You also need a way to prevent spam, we'll use something called a debounce. It's a way for the script to check if we can touch the brick again, it's just a bool.

local Goal = script.Parent
local debounce = true

local function onModelTouched(hit)
	if hit and hit.Parent and debounce then 
		debounce = false
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local character = player and player.Character
		local humanoid = character and character:FindFirstChild("Humanoid")
		-- if player isn't dead
		if humanoid and humanoid.Health > 0 then 
			local leaderstats = player:FindFirstChild("leaderstats")
			local Money = leaderstats and leaderstats:FindFirstChild("Money")
			if Money then
				-- Edit the value, not the object.
				Money.Value = Money.Value + 1
			else
				print (" can't find stats ", leaderstats, Money)
			end
		end
		-- prevent spam 
		wait(1)
		debounce = true
	end
end

Goal.Touched:Connect(onModelTouched)

7 comments

0

Never mind, it doesn't. It now says 18:17:05.315 - Workspace.Goal.MoneyonTouch:15: attempt to perform arithmetic on field 'Money' (a userdata value). I have no clue if this counts as requesting either. But if there is a way, how would I be able to fix this?

0

I edited it, re-paste it.

0

ok

0

It still isn't working, but I think I know what is wrong. Would it be that my leaderstat script is wrong?

0

Works fine for me, so probably. What does the output say?

0

It doesn't say anything for some reason

0

Put a print after each if statement and see what prints and what doesn't. Doesn't make any sense that nothing is printing in your output.


local Goal = script.Parent
local function onModelTouched(part)

local player = part.Parent.Name
local Character = player.Character
player.WaitForChild("leaderstats").Money = player.WaitForChild("leaderstats").Money + 1
Goal.touched:Connect(onModelTouched)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.