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

What's wrong with this? adding cash if touch?

Trying make it so where you touch this brick name Gold, you get 500 cash added onto your leaderboard cash.

print("Servano's Cash Running")

function onPlayerEntered(newPlayer) wait(.5) local stats = Instance.new("IntValue") stats.Name = "leaderstats"

local Cash = Instance.new("IntValue")

Cash.Name = "Cash" Cash.Value = 0

Cash.Parent = stats

--test

GoldTouch = game.Workspace.Gold GoldTouch.Touched:connect(function(hit)

newPlayer.leaderstats.Cash.Value = newPlayer.leaderstats.Cash.Value + 500

end) --test while newPlayer.Character == nil do wait() end

local Player = string.lower(newPlayer.Name)

if Player == "Verocitiy" then Cash.Value = 100000 end

stats.Parent = newPlayer

end

game.Players.ChildAdded:connect(onPlayerEntered)

3 comments

0

What's "Player?" It doesn't exist anywhere else in your case, except on line 2, but even so, your calling it as a value when it's a nil value.

0

look now

1

Codeblock everything plz

2 answers

The problem here is that Player is not defined. You also acces the leaderstats value as Leaderboard, wich will not be displayed.

To get a player in this situation, we could use the GetPlayerFromCharacter function.

This will atempt to get the players datamodel from its character.

To use this, we could do:

game.Workspace.Gold.Touched:Connect(function(hit) -- Touch function
	if hit.Parent:FindFirstChild("Humanoid") then -- This checks if its a Character.
		local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
		Player.leaderstats.Cash.Value  =  Player.leaderstats.Cash.Value  +  500 -- Increase leaderstats by 500.
	end
end

I also have a feeling you have not defined your leaderstats correctly.

Leaderstats can be created by making an object inside of the player named "leaderstats". This can be ANY object. Inside of this object, we put our values, in this case, "Cash", as an IntValue.

To do this we could add the following:

game.Players.PlayerAdded:Connect(function(p) -- Fires when a player joins the game
	local stats = Instance.new("Folder",p) -- Create leaderstats object
	stats.Name = "leaderstats"
	
	local cashV = Instance.new("IntValue",stats) -- Add a cash value inside of leaderstats.
	cashV.Name = "Cash"
end)

If you have any quesitons, feel free to comment below.

If this brought you to a solution, don't forget to accept this awnser and upvote it ;D

You need to define the Player in a different way because hit only returns the part that touched the Gold part. :GetPlayerFromCharacter() is very useful when finding a player by finding its character.

Here, let me fix the script for you:

game.Workspace:WaitForChild("Gold").Touched:Connect(function(hitPart) -- Wait for the part so the script won't error.
	local Player = game:GetService("Players"):GetPlayerFromCharacter(hitPart.Parent)

	if Player then -- Check to see if the Player exists, it could be nil (nothing).
		Player.Leaderboard:WaitForChild("Cash").Value = Player.Leaderboard:WaitForChild("Cash").Value + 500 -- Again... Wait for the child so the script won't think that it never exists.
	end
end)

Any questions? Leave them in the comments! Thanks and I hope this will help you. :)

1 comment

0

Leaderboard isn't a valid thing. It's called leaderstats.

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