Removable roof script won't work

Hello. I made this script where you can remove the roof of a vehicle when pressing a keybind.
The problem is…you guessed it…it wont work.
Its 2 scripts actually.
image
ServerScript: (StowableRoof)

local ls = script.RoofFunction
local event = script.RoofEvent
local roof = script.Parent.Parent.Roof
local player = script.Parent.Occupant
local occ = game.Players:GetPlayerFromCharacter(player.Parent)

if player ~= nil then
	if player.Parent:FindFirstChild("RoofFunction") == nil then
		local clonedscript = ls:Clone()
		clonedscript.Parent = occ.Character
		clonedscript.Enabled = true
	end
end

if game.ReplicatedStorage:FindFirstChild("RoofEvent") == nil then
	script.RoofEvent.Parent = game.ReplicatedStorage
end

local event2 = game.ReplicatedStorage:WaitForChild("RoofEvent")

event2.OnServerEvent:Connect(function()
	if script.Removed.Value == false then
		script.Removed.Value = true
		for _, roof in pairs(roof:GetChildren()) do
			if roof:IsA("BasePart") then
				roof.Transparency = 1
			end
		end
	elseif script.Removed.Value == true then
		script.Removed.Value = false
		for _, roof in pairs(roof:GetChildren()) do
			if roof:IsA("BasePart") then
				roof.Transparency = 1
			end
		end
	end
end)

.
Localscript:


local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")
local event = game.ReplicatedStorage:FindFirstChild("RoofEvent")

local userInputService = game:GetService("UserInputService")

humanoid.Seated:Connect(function(isSeated, seat)
	if isSeated then
		if seat then
			local function onKeyPress(input)
				if input.KeyCode == Enum.KeyCode.R then
					event:FireServer()
				end
			end
			userInputService.InputBegan:Connect(onKeyPress)
		end
	end
end)

Hi there. Is there anything in particular you notice isn’t working properly? Have you first identified what the problem is? Then what the cause is?

I looked in the output.

1 Like

I see this line of code is what the output directs to:
local occ = game.Players:GetPlayerFromCharacter(player.Parent)

player doesn’t exist in that scope. And if it did, you couldn’t get a player object from the parent of a player object. you need the character for what you tried using, GetPlayerFromCharacter().

Edit: You can get the player object from the remote event you fired to the server. event2.OnServerEvent:Connect(function(player)

Alright, I made adjustments to the serverscript using your suggestion.

local ls = script.RoofFunction
local event = script.RoofEvent
local roof = script.Parent.Parent.Roof
local player = script.Parent.Occupant

if game.ReplicatedStorage:FindFirstChild("RoofEvent") == nil then
	script.RoofEvent.Parent = game.ReplicatedStorage
end

local event2 = game.ReplicatedStorage:WaitForChild("RoofEvent")

event2.OnServerEvent:Connect(function(player)
	if script.Removed.Value == false then
		script.Removed.Value = true
		for _, roof in pairs(roof:GetChildren()) do
			if roof:IsA("BasePart") then
				roof.Transparency = 1
			end
		end
	elseif script.Removed.Value == true then
		script.Removed.Value = false
		for _, roof in pairs(roof:GetChildren()) do
			if roof:IsA("BasePart") then
				roof.Transparency = 1
			end
		end
	end
end)

local occ = game.Players:GetPlayerFromCharacter(player)
if player ~= nil then
	if player.Parent:FindFirstChild("RoofFunction") == nil then
		local clonedscript = ls:Clone()
		clonedscript.Parent = occ.Character
		clonedscript.Enabled = true
	end
end

Still doesn’t work, and it did not send an output.

1 Like

Is the remote event firing? Maybe add a breakpoint to the first line inside the remote event function? (event2)

Edit: I’m not sure what script.Removed.Value is all about?

Added a breakpoint. Seems like it isn’t firing because no debugging screen.

It’s a BoolValue.

Why do you set transparency to 1 in both cases? Shouldn’t one be set to 0?

Ah, my bad. It happens sometimes.

but i dont think this is the main problem