You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I am trying to achieve a system that lets you peek out a car window using animations and fire your gun, it works, but there seems to be a bug in the script
What is the issue? Include screenshots / videos if possible!
The issue is that after I exit the car, even when I equip a weapon the animation stills play, keep in mind that the scripts works like this:
local seat = script.Parent
seat.Changed:Connect(function()
if seat.Occupant ~= nil then
local char = seat.Occupant
local ActualChar = seat.Occupant.Parent
char.Parent.ChildAdded:Connect(function(tool)
if tool:IsA("Tool") then
local player = game.Players:GetPlayerFromCharacter(ActualChar)
game.ReplicatedStorage.RightPeak:FireClient(player)
char.Parent.ChildRemoved:Connect(function(tool)
if tool:IsA("Tool") then
game.ReplicatedStorage.StopPeak:FireClient(player)
end
end)
end
end)
end
end)
And here is the local script:
local char = script.Parent
local plr = game.Players.LocalPlayer
RightAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild('PeakCar'))
game.ReplicatedStorage.RightPeak.OnClientEvent:Connect(function(player)
RightAnim:Play()
end)
game.ReplicatedStorage.StopPeak.OnClientEvent:Connect(function(player)
print("ReceivedStopEvent...")
RightAnim:Stop()
end)
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I’ve tried searching everywhere but it seems like nobody has the same problem as me, I’ve also tried disabling the script if the player is not in a sitting position but it didnt seem to work. E.G
if humanoid.sit == false then
script.Parent.Car.Disabled = true
end
If anybody knows a way to fix this please let me know!
Maybe try using the PlayerAdded Event then the CharacterAdded Event to Detect when a tool has been added then run checks to make sure the Player is in a seat rather then doing it inside the seat script.
Heres an Example:
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function(Character)
Character.ChildAdded:Connect(function(Object)
if Object:IsA("Tool") then
if Character.Humanoid.SeatPart.Name == "TheNameOfTheSeatsThatAllowDrivebys" then
-- Fire the client
end
end
end)
end)
end)
The connections need to be disconnected when the player leaves the car.
local seat = script.Parent
local childAddedConn, ancestryChangeConn
seat:GetPropertyCgangedSignal("Occupant"):Connect(function()
if childAddedConn then
childAddedConn:Disconnect()
end
if ancestryChangedConn then
ancestryChangeConn:Disconnect()
end
childAddedConn, ancestryChangedConn = nil, nil
if seat.Occupant ~= nil then
local humanoid = seat.Occupant
local char = humanoid.Parent
local player = game.Players:GetPlayerFromCharacter(char)
childAddedConn = char.ChildAdded:Connect(function(tool)
if tool:IsA("Tool") then
game.ReplicatedStorage.RightPeak:FireClient(player)
tool.AncestryChanged:Wait()
ancestryChangeConn = tool.AncestryChanged:Connect(function()
game.ReplicatedStorage.StopPeak:FireClient(player)
ancestryChangeConn:Disconnect()
end)
end
end)
end
end)
Okay, so I’ve wrote in the script you put, and it seems to be working, except the animations… So I put a print to check if the localscript is receiving the the remotes and it is, but the animations are not working. I think its this part of the local script that isn’t working
PeakCar is the Animation located inside the local script,
And I’ve already added prints() to see what is wrong
So I am guessing there is nothing wrong with the ServerScript located in the actual car. The only problem I’m currently seeing right now is that the animations are not playing, but the localscript is still receiving the remotes from the serverscript
I don’t think the problem is in the local script. Apparently the AncestryChanged event of the tool fires after the ChildAdded event of the character. I added tool.AncestryChanged:Wait() to the code before the connection. Does it now work?