Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a seat for a certain player?

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

✓ Accepted answer

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.

1 comment

0

It didn't work. It still prints "Not owner" but doesn't kick the player out of the chair.

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)

1 comment

0

That didnt work :(

This discussion is preserved as it was. New answers, comments, and votes are closed.