Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you make a coin destroy for only the player that touched it?

Asked by 4 years ago
Edited 4 years ago

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!

0
Sorry, I'm new to scriptinghelpers, but why do the responses gray out, is it because they are editing it? baldi92102 2 — 4y

3 answers

Log in to vote
-1
Answered by 4 years ago
Edited 4 years ago

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!

1
imagine directly giving out code like a weenie instead of explaining it get outta here Fifkee 2017 — 4y
0
Fixed it for you mate :) Utter_Incompetence 856 — 4y
1
I'll try this, thank you! I'll get back to you if it worked or if it didn't! :) baldi92102 2 — 4y
0
Oh, there is an error at the start of the script, it says that HumanoidRootPart is not a valid member of PlayerScripts. Would it work if I just made the variable the local player instead of a part of the player? baldi92102 2 — 4y
View all comments (13 more)
0
yes. my bad Utter_Incompetence 856 — 4y
0
Oh and also it says that touched is not a valid member of the player so it is assuming that im calling for a part inside a player instead of calling for a touch function, so I don't know if this is the way. baldi92102 2 — 4y
0
Use the code I used, I changed it :) Utter_Incompetence 856 — 4y
0
I don't if this is an error with my Roblox Studio because I get this error a lot: Attempt to index nil with WaitForChild baldi92102 2 — 4y
0
And also I received an error that touched is not a valid member of the player, so I assume that roblox thinks I am calling for the child of the humanoidrp baldi92102 2 — 4y
0
Are you putting the script in StarterCharacterScripts? Utter_Incompetence 856 — 4y
0
Yes baldi92102 2 — 4y
0
oh wait im dumb, I forgot a ), should work now! Utter_Incompetence 856 — 4y
0
i added at the beginning i am dumb i was putting it in starter player scripts but now still it is only working for 1 coin not the rest. baldi92102 2 — 4y
0
did you use the new code? Utter_Incompetence 856 — 4y
0
Yes I did. baldi92102 2 — 4y
0
Should I make yours at the answer? baldi92102 2 — 4y
0
yes thanks Utter_Incompetence 856 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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.

0
*DISCLAIMER* I marked Utter's response as the answer because it gives the concept on how you could answer my question. If you are looking for something more precise, please look at my answer. Thank you all for your responses! I really appreciate it! baldi92102 2 — 4y
Log in to vote
-2
Answered by
Xapelize 2658 Moderation Voter Community Moderator
4 years ago

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.)

0
If the print statement wrong, just delete it, cuz it was useless. Btw, if this worked for you, mark this as the answer so we both can get points :) Xapelize 2658 — 4y
0
Yea thank you for this but from Utter's script, is it right to reference the humanoid root part for being touched? baldi92102 2 — 4y
0
Oh nevermind that question I understood what you are doing but I don't understand how it only destroys the coin for the player who touched the coin and not everybody in the coin. If you could explain a little bit more I would really appreciate it! Thank you for your response! baldi92102 2 — 4y
0
He wanted it to destroy it for the person who touched it though? He didn't want to make multiple scripts, too. Also baldi: You could use any body part you wanted, I just used HumanoidRootPart as an example. Utter_Incompetence 856 — 4y
View all comments (4 more)
0
@baldi92102 they sensors every SINGLE part in your body, includes torso, legs, hand, and also humanoid root part too! But this script is most used for the player's choiced avatar type. Xapelize 2658 — 4y
1
I'll try this guys thank you for your responses! baldi92102 2 — 4y
1
Utter it is fine if there is one script inside each of the coins i just don't want to rename all the coins and make local scripts for each of the coins separately. baldi92102 2 — 4y
0
Um.... why would you not use a local script... use a local script... TiredMelon 405 — 4y

Answer this question