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

How To Get The Player In A Normal Script?

Asked by 5 years ago

Heres My Attempt

--Control Script
local player = script.Parent.Parent
script.Parent.Touched:connect(function(hit)
local tool = game.Lighting.Slide:Clone()
tool.Name = "Control"..hit.Parent.Name
tool.Parent = player.Name
end)

0
Un, what's a normal script? Also, you shouldn't use Lighting as storage. User#19524 175 — 5y
0
can you explain what is going on a little better User#21908 42 — 5y
0
I edited my answer for you @basstracker1970 User#21908 42 — 5y
0
Please properly indent your code as it makes it easy to read and debug. User#21908 42 — 5y

4 answers

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

From what I am reading I think you are using a LocalScript. If so all you need to do is get the local player like so:

local players = game:GetService("Players")
player = players.LocalPlayer

Hope this helps and have a great day scripting!

Edit 1: If the script is a descendant of a player then use a LocalScript. Otherwise you will have to get the player either by looping through the players and applying it to each one, or getting the player via an event such as the PlayerAdded or Touched events. Also as the other answers point out, you should us something other than Lighting for storage and :Connect instead of :connect.

0
no u User#19524 175 — 5y
0
Yes, me seith14 206 — 5y
0
im useing a normal script a non local script basstracker1970 -29 — 5y
0
Also as the other answers point out, you should us something other than lighting for storage and :Connect instead of :connect. User#21908 42 — 5y
View all comments (2 more)
0
@incapaz what do you mean? User#21908 42 — 5y
0
In one of my recent projects I got the player using a remote event, if that helps. MrBlockyhead 84 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
game.players.playeradded:connect(Function (plr)

end)

plr is now your player.

0
Fail. User#19524 175 — 5y
0
Yea, a total fail, it should work, if it doesn't then someone can correct me... Twenty1chickens 4 — 5y
0
I'll tell you the errors. 1. game.players errors because an object named players isn't inside game. 2. playeradded is not a valid member of Players, 3. connect should be Connect 4. Function should be function 5. there should be no space between function and (plr) User#19524 175 — 5y
0
Lel, 5 errors in only 2 lines... AswormeDorijan111 531 — 5y
0
Wow User#21908 42 — 5y
Log in to vote
0
Answered by
Sorukan 240 Moderation Voter
5 years ago

Script.Parent.Parent can only be used when you insert the script inside a tool in the StarterPack. Since you're using a Touched event, what you can do is

script.Parent.Touched:connect(function(hit)

if hit.Parent then
 local player = game.Players:GetPlayerFromCharacter(hit.Parent) ---now player is defined
 if player then 
 local tool = game.Lighting.Slide:Clone()
 tool.Name = "Control"..player.Name
 tool.Parent = player.Name
        end
    end
end)

also don't put your script in lighting, i'm just showing you what the script should be like.

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Using a thousands of Parent's is bad practicing! You should be using:

script.Parent.Touched:Connect(function(char)
local plr = game:GetService("Players"):GetPlayerFromCharacter(char.Parent)
end)

Also you're using :connect() which is deprecated, you should be using :Connect(). Using a Lighting as your storage is bad, you should be using ReplicatedStorage! Here,

Script:

-- Control Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")

-- Put your tool inside a ReplicatedStorage!

script.Parent.Touched:Connect(function(hit)

local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)

local tool = ReplicatedStorage:WaitForChild("Slide"):Clone()

tool.Name = "Control"..hit.Parent.Name
tool.Parent = plr.Name
-- What are those two lines up here?^

end)

Hope that I helped you!

Answer this question