I’m currently working on an RPG to see what I can do with scripting and I’m still lost on this even though I’ve been scripting for over four months. I don’t really know how to word this correctly, but if I was making a shop GUI, how would I figure out which player to give the tool to? If a player touches a button, how do I figure out which player it was? Simple stuff like that is what I don’t understand. If the game is singleplayer, should I use LocalScripts or scripts? If it was multiplayer, which now? What do you do differently for singleplayer and multiplayer?
Can you explain it more detailed I can’t seem to understand what are you talking about?
In a LocalScript, you can say Players.LocalPlayer to quickly find the Local Player and it’s super easy. How would you do something like this in a normal script?
The way you would go about getting the Player depends completely on what you’re doing.
If you were working with GUIs, a shop for instance, then that means you must be working from the client( using LocalScripts ). This would mean you could use “game.Players.LocalPlayer” to access the Player object connected to this client the LocalScript is being run on.
If you’ve got a ServerScript in which something happens when a brick is Touched, per say, you couldn’t get the .LocalPlayer anymore since the Server is not linked to one player in particular. You would need to get the Player by looking at the object that collided with your part, checking if it is within a Character, and then getting the Player connected to that Character.
Part.Touched:Connect(function(part)
if part.Parent:FindFirstChild("Humanoid") then
local Player = game.Players:GetPlayerFromCharacter(part.Parent)
end
end)
Once again, the method in which you get the Player, would differ depending on what you’re doing though.
Hopefully this clears things up a little.
Also, four months is not at all a long time to have scripted, so don’t feel bad if you don’t understand something.
in the first argument of the touched function, it will have an argument of what part pressed on it, you can get the parent of the part and get the player from that character (or parent let’s say) then you get or know the player who did it
for guis, you can use game:getservice(“players”).localplayer and it still works
use the format of the code given below
place a script inside the part which the player must touch to get the tool
script.Parent.Touched:Connect(function(touched)
if touched.Parent:FindFirstChild("Humanoid") then
--The code where you give the player A tool
end
end)
how would I figure out which player to give the tool to?
This depends if you are using Gui’s then you would use game.Players.LocalPlayer
if you are using Touched events you can find the character with this
Part.Touched:Connect(function(hit)
if hit:IsA("BasePart") then
Player = game.Players:GetPlayerFromCharacter(hit.Parent)
end
end)
if you are using Click Detectors then the player who clicked it is passed as the first parameter
ClickDetecor.MouseClick:Connect(function(player)
Simple stuff like that is what I don’t understand. If the game is singleplayer, should I use LocalScripts or scripts? If it was multiplayer, which now? What do you do differently for singleplayer and multiplayer?
Always use scripts whenever possible local scripts should only be used in cases like Gui buttons, Getting input from a player, etc…
If its single player then you might be able to get away with using Local Scripts a bit more often