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

How to make a seat for a certain player?

Asked by
Zrxiis 26
7 years ago

I tried to make a script for a seat so that only the player whose userId is in the seat can sit on it, but it stilll lets other player sit in the seat and out put shows no error

local seat = script.Parent.Saddle
local identifier = script.Parent:WaitForChild('identifier')
seat.Changed:connect(function(property)
    local occupant = seat.Occupant
    if occupant then
        local character = occupant.Parent
        local player = game.Players:GetPlayerFromCharacter(character)
        if player then
            if player.UserId ~= identifier.Value then
                character.Humanoid.Sit = false
            elseif player.UserId == identifier.Value then
                return end
            end
        end
    end
end)

2 answers

Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

Your code should make an error as you have an extra end.

local seat = script.Parent.Saddle
local identifier = script.Parent:WaitForChild('identifier')
seat.Changed:connect(function(property)
    local occupant = seat.Occupant
    if occupant then
        local character = occupant.Parent
        local player = game.Players:GetPlayerFromCharacter(character)
        if player then
            if player.UserId ~= identifier.Value then
                character.Humanoid.Sit = false
            elseif player.UserId == identifier.Value then
                return end -- You have added another end here ??
            end
        end
    end
end)

Lastly we should check for what is being changed in the seat then run the code.

Example (updated):-

local seat = script.Parent
local identifier = script.Parent:WaitForChild('identifier')

seat.Changed:connect(function(prop)
    if prop == 'Occupant' and seat.Occupant then -- we only run the code when we change the occupant
        local plr = game.Players:GetPlayerFromCharacter(seat.Occupant.Parent) -- the players model
        if plr then
            if plr.UserId ~= identifier.Value then
                local weld = seat.ChildAdded:Wait()
                repeat wait() until weld.Parent -- not the best solution
                weld:Destroy()
            end
        else
            print('Plr not found')
        end
    end
end)

The problem with this is that we need to wait for the weld and for it to be a parent of the seat. This process is all managed by roblox so until it has finished we cannot move the player or it will weld the player to the seat even if you make them jump.

I hope this helps.

0
It didn't work. It still prints "Not owner" but doesn't kick the player out of the chair. Zrxiis 26 — 7y
Ad
Log in to vote
-1
Answered by 7 years ago
local seat = script.Parent.Saddle
local identifier = script.Parent:WaitForChild('identifier')
seat.Changed:connect(function(property)
    local occupant = seat.Occupant
    if occupant then
        local character = occupant.Parent
        local player = game.Players:GetPlayerFromCharacter(character)
        if player then
            if player.UserId ~= identifier.Value then
                character.Humanoid.Jump = true
            elseif player.UserId == identifier.Value then
                return end
            end
        end
    end
end)

0
That didnt work :( Zrxiis 26 — 7y

Answer this question