So, I am creating this 2D Platformer game and it includes the usage of coins. The problem is that whenever a player touches a coin, that coin destroys for all players. So, other people can't get that coin. Is there any way you could make the coin so it destroys for only the player who touched it and not everybody. I want to do this without renaming all the coins and using several scripts. Thank you guys!
You'd use a LocalScript
What's a LocalScript?
A LocalScript is a special Instance of script. Key difference being that whatever you code within it only appears on one client.
Woah woah woah, What's an Instance?
An Instance is a class object, such as the mention LocalScript. Each instance has it's own properties. Parts have the Part_Instance.Touched function which fires whenever it collides with another Part instance. We can apply this here;
--Put in StarterCharacterScripts local hrp = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart") --You can change the body part --game.Players.LocalPlayer can only be used in LocalScripts! hrp.Touched:Connect(function(hit) if hit.Name == "Coin" then --You can change the name --code hit:Destroy() end end)
Yea I know you're asking: What's :Destroy() do?
Instance:Destroy() is a function of all things! Just some instances can't be destroyed, like services and game. It simply deletes the instance from existence.
OH DEAR! What's a service?
A service is a what you see in your explorer window, being stuff like ReplicatedStorage and/or Workspace. There's also some which you can't see like UserInputService. Each service has it's own important use in games. ReplicatedStorage is where you'd put things used by both the client and server; like RemoteEvents and RemoteFunctions. ServerStorage is used to hold objects used by the server; say models.
If I must elaborate on something, do let me know! I know my pal here Fifkee needed a better explanation, so maybe you do too!
So guys, I found a way to make the coin destroy for only the player that touched it, and yet make it so everybody can see your coins In the leaderboard. Here's my scripts. Please read the comments for a full explanation of what is going on.
My LocalScript in StarterCharacterScripts:
local hrp = game.Players.LocalPlayer.Character:WaitForChild("Humanoid") local player = game.Players.LocalPlayer local chicken = false local Event = game:GetService("ReplicatedStorage").GoodCoins hrp.Touched:Connect(function(hit) if hit.Name == "Coin" then -- If something hits the humanoid, it will see if its name is "Coin" if chicken == false then -- If Chicken is false,(which it is), then the code below will run chicken = true -- Chicken then becomes true, so if you don't set it back to false, the next coin would not be collected. Event:FireServer() -- We are firing this remoteevent to the server, so the whole server can see it print('POINTS: ' .. player.leaderstats.Points.Value) -- Optional just to print in the output print(hit.Name) -- Optional, again hit:Destroy() -- Now destroy the coin wait(0.1) -- Wait to ensure the player doesn't get too many points for one coin. chicken = false -- We are setting chicken back to false, so the code can run again. end end end)
My script in ServerScriptService:
local Event = game.ReplicatedStorage.GoodCoins -- The event Event.OnServerEvent:Connect(function(player) -- The code underneath this will be firing whenever the event is fired (in the local script) local Points = player.leaderstats.Points -- Just assigning a variable, to use player make sure one of the function's parameters is player. Points.Value = Points.Value + 1 -- We are going to add a point everytime they collect the coin. end)
So, guys there may be some code that you need to change for it to work according to you. Make sure you add a remoteevent in Replicated Storage and apply the name accordingly to your script. You can also name the coins anything you want, but please make sure to put the name you want at line 7. I will give the answer to Utter's response as that helped me understand the concept. If you need any help, you can comment on this answer.
Sorry, but @Utter_Incompetence's explainment is TOTALLY goes wrong.
DO NOT USE A LOCAL SCRIPT BECAUSE LOCAL SCRIPT ONLY FIRES FOR THE LOCAL PERSON. But @Utter_Incompetence's code doesn't gone wrong, just do some adjustments:
(Utter_Incompetence's script)
--StarterCharacterScript local hrp = script.Parent.HumanoidRootPart hrp.Touched:Connect(function(hit) if hit.Name == "Coin" then --code hit:Destroy() end end
What I changed:
function OnTouched(function(hit) print(.. hit.Name.. "have touched the coin!") script.Parent:Destroy() end) script.Parent.Touched:Connect(function(hit)
It make a function that sensors if the people touched it, and print who touched it and remove itself.
(P.S: Please put the script inside a coin, and it was normal script.)