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

Awarding coins through an event not working correctly?

Everytime the "Win" part is touched, it comes up with the same error: ServerScriptService.EventHandler:3: attempt to call method 'FindFirstChild' (a nil value) This also goes for "WaitForChild"

If you are wondering, without the "FindFirstChild" or the "WaitForChild", it comes up with this: ServerScriptService.EventHandler:3: attempt to index field 'leaderstats' (a nil value)

Here's my line of code:

game.ServerScriptService.EventHandler

game.ReplicatedStorage.Events.Coins.Event:connect(function(player, coins)
	wait()
	player:FindFirstChild("leaderstats").Coins.Value = player:FindFirstChild("leaderstats").Coins.Value + coins
end)

game.Workspace.Lobby.ObbyParts.Win.AwardCoins

local part = script.Parent.Parent.Teleport

script.Parent.Touched:connect(function(touch)
	if touch.Parent:FindFirstChild("Humanoid") then
		local player = touch.Parent.Name
		local difficulty = game.Workspace.Lobby.ObbyParts.Map:FindFirstChildOfClass("Model").Difficulty.Value
		print(player.." has beaten the "..difficulty.." obby!")
		if difficulty == "Easy" then
			game.ReplicatedStorage.Events.Coins:Fire(player, 5)
		elseif difficulty == "Medium" then
			game.ReplicatedStorage.Events.Coins:Fire(player, 10)
		elseif difficulty == "Hard" then
			game.ReplicatedStorage.Events.Coins:Fire(player, 25)
		elseif difficulty == "Very Hard" then
			game.ReplicatedStorage.Events.Coins:Fire(player, 50)
		elseif difficulty == "Insane" then
			game.ReplicatedStorage.Events.Coins:Fire(player, 100)
		end
		wait()
		if touch.Parent:FindFirstChild("UpperTorso") then
			touch.Parent.UpperTorso.CFrame = part.CFrame
		elseif touch.Parent:FindFirstChild("Torso") then
			touch.Parent.Torso.CFrame = part.CFrame
		end
	end
end)

Any help appreciated, thanks.

1 answer

✓ Accepted answer

In your AwardCoins script, player is set to the string of the player's name. You then send this value to the EventHandler script, which expects the player object. If you remove the .Name on line 5 of your AwardCoins script, player will be the correct value (though you may need to adjust your print statement to player.Name).

Note that if you intend to use leaderstats without checking if it exists, you don't need to use FindFirstChild (though WaitForChild still makes sense if you expect that it could be added shortly -- though in this case, if the leaderstats haven't been added by the time the player has finished part of your game, I would not expect them to ever load, meaning WaitForChild won't likely help you here), meaning you can just do player.leaderstats.Coins.Value.

[EDIT]

Just realized that your script says player = touch.Parent.Name -- touch.Parent is likely to be a model. What you probably want is player = game.Players:GetPlayerFromCharacter(touch.Parent) or game.Players:GetPlayerFromCharacter(touch.Parent.Parent).

4 comments

0

I did everything you told me to, and it comes up with an error: "ServerScriptService.EventHandler:3: attempt to index field 'leaderstats' (a nil value)", the same when I removed "WaitForChild" or "FindFirstChild" on the same line.

0

That means 'player' is still a string. Use print commands to help figure out what's going on; you can print(type(player)) to make sure that it's "userdata" instead of "string". Double check the information you're sending to the event.

0

The first time I used "print(type(player))", it printed "userdata", but still came up with the same error.

0

Edited the answer - try what I suggest at the bottom. If that still doesn't work, try printing "print(typeof(player))" and "print(player.ClassName)"?

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