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

I made Touched Event, it errors out, why?

Local Script Lines 75 - 79:

workspace.PickupPistol_1.Touched:Connect(function(hit)
	if hit then
		game.ReplicatedStorage.PistolPickupEvent:FireServer(hit)
	end
end)

Local Script Lines 81 - 87:

workspace.PickupRifle_1.Touched:Connect(function(hit)
	if hit then
		game.ReplicatedStorage.RiflePickupEvent:FireServer(hit)
	end
end)

Server Script Lines 45 - 52:

pistolpickup.OnServerEvent:Connect(function(plr, hit)
	if hit.Parent.Humanoid then
		game.ReplicatedStorage.GunSwapBool.Value = true
		game.ReplicatedStorage.PistolEquipped.Value = true
		game.ReplicatedStorage.RifleEquipped.Value = false
		game.ReplicatedStorage.GunSwapRifleBool.Value = false
	end
end)

Server Script lines 54 - 61:

riflepickup.OnServerEvent:Connect(function(plr, hit)
	if hit.Parent.Humanoid then
		game.ReplicatedStorage.GunSwapRifleBool.Value = true
		game.ReplicatedStorage.PistolEquipped.Value = false
		game.ReplicatedStorage.RifleEquipped.Value = true
		game.ReplicatedStorage.GunSwapBool.Value = false
	end
end)

5 comments

0

error: ServerScriptService.BackendTest:55: attempt to index nil with 'Parent'

0

So the error is on the 55th line?

0

are these the full scripts?

0

Show us the script "BackendTest" at lines 40-60

0

lines 50 - 57:riflepickup.OnServerEvent:Connect(function(plr) game.ReplicatedStorage.GunSwapRifleBool.Value = true game.ReplicatedStorage.PistolEquipped.Value = false game.ReplicatedStorage.RifleEquipped.Value = true game.ReplicatedStorage.GunSwapBool.Value = false end)

1 answer

Instead of having the check for humanoid in the serverscript, why don't you make the check in the localscript, and fire the remote only when hit.Parent is a humanoid. That way you can get rid of the hit parameter in serverscript that's giving you the error. Try that and see what happens.

Localscript

workspace.PickupPistol_1.Touched:Connect(function(hit)
    if hit.Parent.Humanoid then
        game.ReplicatedStorage.PistolPickupEvent:FireServer(hit)
    end
end)

Serverscript

pistolpickup.OnServerEvent:Connect(function(plr)
        game.ReplicatedStorage.GunSwapBool.Value = true
        game.ReplicatedStorage.PistolEquipped.Value = true
        game.ReplicatedStorage.RifleEquipped.Value = false
        game.ReplicatedStorage.GunSwapRifleBool.Value = false
    end
end)

2 comments

0

ok ill try that

0

it doesn't work when in local script

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