I have an IntValue called leaderstats in the player and another IntValue called coins in leaderstats. I have a part that when touched I want it to add 10 to the Coins. Here is my script
local giver = script.Parent local player = game.Players.LocalPlayer local function SteppedOn(part) player.leaderstats.Coins.Value + 10 end giver.Touched:connect(SteppedOn)
I can't figure out what I did wrong.
I assume you are using a Server Script. LocalPlayer
is for LOCAL SCRIPTS ONLY!!! So, if you want to find out who the player is, then you need to do it in another way. Think of it: If someone else that is not you steps on the giver, then you'll still earn the points! Also, the character in Workspace is a model, and the Touched event only returns the part that touched (triggered) it. So, you need to refer to its parent in order to get the player.
Apart from that, in line 5, you are trying to change the leaderstats the wrong way. Let me help you fix it.
Here is the script:
local giver = script.Parent local db = false local function SteppedOn(part) if not db then -- UPDATE: Added debounce! Very important for events like touched so it doesn't run multiple times! db = true local player = game.Players:GetPlayerFromCharacter(part.Parent) -- Get the player by the part that hit the giver. if player then -- Check to see if a player is found. player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 10 -- Change the value by adding 10 to it. end wait(1) db = false end end giver.Touched:connect(SteppedOn)
Any questions? Leave them in the comments below! Thanks! :)
If I am right, LocalPlayer cannot be accessed through a Server Script. To access the player find the Player through the Character touching the Part.
Also a Debounce might be useful to you, otherwise touching the Part will spam the function multiple times.
GetPlayerFromCharacter: http://wiki.roblox.com/index.php?title=API:Class/Players/GetPlayerFromCharacter
Debounce: http://wiki.roblox.com/?title=Debounce
Your problem is simple. You're trying to add to a value, but you're not actually setting the value.
Here's the fixed code:
local giver = script.Parent local player = game.Players.LocalPlayer local function SteppedOn(part) player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 10 end giver.Touched:connect(SteppedOn)