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.
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)
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.