Im new to scripting and ive been struggling with making a part that adds 10 coins whenever you touch it. I actually did it but even when another player stepped on it, it added it to my coins and its been bothering me because I have no idea how to fix it.
Consider posting your original code, it’s better for your learning progression if we point out what’s wrong and what you can improve than if someone else writes the whole code for you!
Alright so it would be best to post your original code to help people figure out what exactly went wrong.
Want to ask for the following if it may help you with finding your solution:
-
if your Touched:Connect(function(part)) function has a condition to where if the part that touches it contains a humanoid (a simple “if part.Parent:FindFirstChild(“Humanoid”) then” conditional statement)?
-
If you have it to where it gets the player from the character model (Basically “local chr = touch.Parent” and then “local plr = game:GetService(“Players”):GetPlayerFromCharacter(chr)” to get the specific player’s information which from there you’d be able to configure the leaderstats)
Thx!
Sorry I forgot to post the code, here it is:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
Coins.Value = 0
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
Coins.Value = Coins.Value + 10
script.Parent:Destroy()
end
end)
end)
Its so scuffed
Sorry I forgot to post the code, here it is:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Parent = leaderstats
Coins.Value = 0
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
Coins.Value = Coins.Value + 10
script.Parent:Destroy()
end
end)
end)
Ik its scuffed but at the time I was happy with it
Is leaderstats a folder contained in the player already? You need to create the object if it doesn’t already exist and add this code into a function that’s conntected to a PlayerAdded event.
OK so first things first. I would not recommend having script.Parent.Touched() be included as a part of PlayerAdded. It would be more practical at least to me to approach it as 2 seperate functions so…
-- Put in ServerScriptService
-- Put in ServerScriptService
local part = game.Workspace.Part
game:GetService("Players").PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Coins = Instance.new("IntValue")
Coins.Name = "Coins"
Coins.Value = 0
Coins.Parent = leaderstats
end)
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local chr = hit.Parent
local plr = game:GetService("Players"):GetPlayerFromCharacter(chr)
if plr then
plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value + 10
part:Destroy()
end
end
end)
With this at least you wouldnt need to delete the script anymore (Cause it was being deleted alongside the part) which could also be a potential source of problem?
local PS = game:GetService("Players")
PS.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local coinValue = Instance.new("IntValue")
coinValue.Parent = leaderstats
coinValue.Name = "Coins"
end)
And you would connect a .Touched event to a coin like you are above, this is assumimg a script in the coin itself:
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player = PS:GetPlayerFromCharacter(hit.Parent)
local playerLeaderstats = player:FindFirstChild("leaderstats")
local Coins = playerLeaderstats:FindFirstChild("Coins")
Coins.Value = Coins.Value + 10
script.Parent:Destroy()
end
end)
The only thing we’re doing differently here is creating the leaderstats folder inside of your player, then incrementing its value when the character touches the part, then destroy the coin. Your old solution would have destroyed the player if the script was in the player.
Make sure this is all in scripts on the server side, otherwise the client (player) can manipulate these values, hence isnt a definite source of truth (this includes creating and spawning the coins on the server, and binding the .Touched connectes function on the server as well).
Right so you’ll want to get the name of the player who touched it (hit.Parent.Name, the name of the player chracter), and you’ll want to edit the leaderstats only for that person, so for example
game.Players[playername].leaderstats.Coins.Value +=10
Also make sure you’re doing this in a script, and not a LocalScript!
I have question. What is the difference between game.Players.PlayerAdded:Connect(function(player)
and
game:GetService(“Players”).PlayerAdded:Connect(function(player)
Can I add this function only for the client
They perform the same way honestly but with :GetService(), its just a safe way to call these services that aren’t guaranteed to be existing in the game atm. If ever there is the occasion that you would choose to rename the Players service from your workspace (For example: “PlayersInGame”), game.Players would result in an error as Players doesnt exist (cause you renamed it to PlayersInGame), but with game:GetService(“Players”), no matter what you would name it to, it will know that it is looking for the game’s player service.
You can also read up more about it here: game.Players V.S. game:GetService("Players") Whats the difference? - #3 by x86_architecture