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

How to use a variable to identify a player?

Asked by 4 years ago

I've tried to define a variable "occupant" to act as the player, code I used (with all essential parts included) solutions would be appreciated!

local seat = script.Parent
local data = seat.Configuration
local owner = nil
local running = false
local Users = game.Players
local x=0
local y=0


seat.ChildAdded:connect(
function(w)
if w.className=="Weld" and w.Name=="SeatWeld" then
local char=w.Part1.Parent
print(char)
local occupant = game.Players["char"] --Here is the problem code line.
print(occupant)
local mouse = occupant:GetMouse()
print(mouse)

local player=game.Players:FindFirstChild(char.Name)
if player~=nil then
owner=player
seat.ChildRemoved:connect(function(w2)
if w2==w then
owner=nil
end
end)

local spd = 0
local gui = seat.ScreenGui:clone()
seat.BodyVelocity.maxForce=Vector3.new(1,1,1)*data.MaxForce.Value
seat.BodyGyro.maxTorque=Vector3.new(1,1,1)*50000
gui.Parent = player.PlayerGui

while owner==player do
seat.BodyVelocity.velocity=seat.CFrame.lookVector*spd
wait()
end
gui:remove()
seat.BodyVelocity.velocity=Vector3.new(0,0,0)
seat.BodyVelocity.maxForce=Vector3.new(0,0,0)
seat.BodyGyro.maxTorque=Vector3.new(0,0,0)
end
end
end)

mouse.KeyDown:connect(function()
    print("test")
end)
  bailey_chap
  12:58:34.602 - char is not a valid member of Players
12:58:34.602 - Stack Begin
12:58:34.603 - Script 'Workspace.PlaneSeat.Script', Line 18
12:58:34.603 - Stack End

1 answer

Log in to vote
0
Answered by
Jexpler 63
4 years ago

You're doing game.Players then a string value. You need the actual player. To get an object with a specific string value as a name do game.Players:FindFirstChild("char")

0
Just remove these "" around char and you will be good http_shawn 1 — 4y
Ad

Answer this question