Drive-by script broken

You can write your topic however you want, but you need to answer these questions:

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

  1. 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:

You enter the car, it detects who’s in the seat,and if the player pulls out a tool it plays a animation.
This is what it looks like:
https://gyazo.com/d47d53c3d6390a8a7964d3a024d917b5

This is the bug I’m dealing with:
https://gyazo.com/ba954dadae13347418bdc3810ef31c47
(This only happens after I’ve entered the car & the animation has played.)

Here is the serverscript:

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

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

RightAnim = char:WaitForChild("Humanoid"):LoadAnimation(script:WaitForChild('PeakCar'))

or this part

game.ReplicatedStorage.RightPeak.OnClientEvent:Connect(function(player)
	print("ReceivedAnimation")
	RightAnim:Play()
end)

but I am not too sure why the localscript isn’t working.

Couple things to note:

  • What is peakcar defined as? It should be some sort of string or number…?

  • You don’t need to use OnClientEvent for passing a player argument, just leave the tuple arguments as blank

  • You could also check by adding some print() statements to confirm where the issue may lie :thinking:

PeakCar is the Animation located inside the local script,
image
And I’ve already added prints() to see what is wrong
image
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?

1 Like

Yes it took me a long minute to realize it wasn’t the local script and now it works perfectly. Thank you so much!