Issue with Remote Event

the thing i am trying to achieve - i use remote event to deal damage to enemies and to destroy boxes

it has been working until i added the box, when the player destroys the box then dies, the slide(how i deal damage) no longer works,

ive tried messing around with it but to no avail so far here is the issue in video form
robloxapp-20240206-1841398.wmv (2.7 MB)

i also get this error once i die
Screenshot 2024-02-06 184230

it seems to me everything is connected as sometimes killing one npc will kill others and destroying the box can have the same result
here is the script, first function for the npc killing the second for the box destroying

function SlideDmg(hit)

	local Bob = game.Workspace["Level 1"].Enemies.Bob
	Character = hit.Parent

	if hit.Parent and SlideTrack.IsPlaying then
		game.ReplicatedStorage.DamageEvent:FireServer(Bob)
		task.wait()
	end

end


function destroyBox(hit)

	Character = hit.Parent
	if hit.Parent:FindFirstChildOfClass("Humanoid") and SlideTrack.IsPlaying then
		game.ReplicatedStorage.BoxEvent:FireServer(box)
	end
	
end

the other part of the remote events

local coinBindable = game.ReplicatedStorage.CoinBindable


game.ReplicatedStorage.DamageEvent.OnServerEvent:Connect(function(Player, Bob)
	
	Bob:WaitForChild("HumanoidRootPart").ParticleEmitter.Enabled = true
	task.wait(0.5)
	
	Bob.Parent = game.ServerStorage
	
	wait(10)
	Bob:WaitForChild("HumanoidRootPart").ParticleEmitter.Enabled = false
	Bob.Parent = game.Workspace["Level 1"].Enemies.Bob

end)



game.ReplicatedStorage.BoxEvent.OnServerEvent:Connect(function(Player, box)
	task.wait(0.5)

	box:Destroy()
	coinBindable:Fire()


end)


the line that errors is the touched event for the box this is obviously because it no longer exists but the error only fires once the player dies

edit i dont know if this part is relevant but since it involves the slide function also here is that just in case

local function Slide(input, _gameProcessed)
	if _gameProcessed then return end
	if input.KeyCode == Enum.KeyCode.LeftShift and Humanoid.MoveDirection.Magnitude > 0 then

		if debounce then
			return
		end
		debounce = true
		Humanoid.JumpPower = 0
		WalkTrack:Stop()
		task.wait(0.1)
		SlideTrack:Play()
		if SlideTrack.IsPlaying then
			LFEmitter.Enabled = true
			RFEmitter.Enabled = true
		end
		task.wait(1)
		SlideTrack:Stop()
		Humanoid.JumpPower = 50
		debounce = false

		if SlideTrack.IsPlaying == false then
			LFEmitter.Enabled = false
			RFEmitter.Enabled = false
		end
		


	end
end

Judging by how it “errors when you die”, and the folder you are trying to get is located in the character; I can make an educated guess that your code only runs once.

If you want to fix this, I suggest you utilize CharacterAdded.

If you have problems with the event not firing, you may want to put the event inside a PlayerAdded event.

it only happens if i break that box though, i havent cloned that part so it will error since it no longer exists once i destroy it.
so if i break the box it will not error then once i die it will make the error then the slide function stops working
if i take the box out of the game for now, the slide function will keep working
the other issue is when destroying the box or and npc may delete a different one
i think it would have more to do with the remote event itself

i wonder if its possible that since things are kinda connected together wrongly somehow if the slide function is getting deleted also

It will also error because the box is destroyed when the character disappears & respawns, instances that the humanoid is not familiar with will be lost, thus deleted.

A lazy suggestion, but try using pcall(function() when destroying an object if you are unsure that it will constantly exist. pcall() will silence any errors, and still run the code with the exclusion of errored line in the function. (based on my observations, at least)

Also, instead of doing:

wait(1) -- 1/30 per tick, replace with task.wait() -- 1/60 per tick 
-- (keep in mind replacing wait() with task.wait() is very situational & 9 times out of 10 it's optional)
part:Destroy()

Replace it with this

game:GetService("Debris"):AddItem(Part, 1) -- Parameters: Target Instance, Life Expectancy

i see what you mean. i’ll give a quick update since i just got back to it
i took the box out the game and dumped it in serverStorage, deleted the code to do with the box except the variable for the box
slide function wouldnt work on joining the game
worked when i deleted the variable.
i dont think pcall() would be necessary but worth a try i know the part wont exist because there isonly one and i destroy it so there isnt another one since i didnt clone it

im going to test if its the same on the enemy npcs if it is then the remotes are wrong if not then i connected it the wrong way (my assumptions)
edit
well im glad i did that because it seems everything is connected all wrong take a lookie
robloxapp-20240207-1330463.wmv (2.1 MB)
second edit
so turns out my hit.Parent was wrong so i switched it out now the enemies arent doing the thing in the vid
now i will add the box back and see what happens
another edit
now the box doesnt destroy when i slide into it but destroys when i kill an npc
robloxapp-20240207-1439033.wmv (1.7 MB)
this is also now appearing
Screenshot 2024-02-07 145617
i dont know why its trying to look for a HumanoidRootPart for something that doesnt have one
this error now changed to the level area folder and not the boxes folder

apparently just making the part a model fixed it, was able to be more specific with it then so instead of just hit.Parent i added

if hit.Parent:FindFirstChild("PointsCrate")

I was lazy, so instead of brainstorming to figure out your problem, you can just read & utilize this function. I noticed DamageEvent & BoxEvent had two separate function events, so I combined them both into one event function to save memory & be more efficient.

I made excessive comments on the script so you can understand what’s going on, or if you wanna know my thought process. I didn’t really optimize the script and kinda just did what you did, but only reformatted it.

local ReplicatedStorage = game:GetService('ReplicatedStorage')
ReplicatedStorage.DamageEvent.OnServerEvent:Connect(function(plr, opp)
	-- Gets character, and makes a new variable with a default spawn rate of 8.
	-- This number is changed based on wether or not "opp" has a humnanoid or not. Set to 10 if it's an enemy, set to 5 if it's a box.
	local char, spawnrate = plr.Character or plr.CharacterAdded:Wait(), 8

	-- Checks if it has a humanoidrootpart or not, so it doesn't make false checks.
	if opp:FindFirstChild("HumanoidRootPart") then
		opp['HumanoidRootPart'].ParticleEmitter.Enabled, spawnrate = true, 10

		-- Runs the function in a separate thread, does not yield code.
		task.delay(.5, function()
			opp['HumanoidRootPart'].ParticleEmitter.Enabled = false
			opp.Parent = game:GetService('ServerStorage')
		end)
	-- Checks if it's a box, read further down to understand why I added this.
	elseif opp.IsBox then
		opp.Parent, spawnrate = game:GetService('ServerStorage'), 5	
		-- Put CoinBindable Function Here.
	end

	wait(spawnrate) -- task.wait() is unneccessary, so we'll just use the normal wait function instead.
	-- Judging how your code is formatted, the only humanoid that could be affected by this event was "bob".
	-- This loop gets every instance in ServerStorage, and searches for one that matches "opp" or "bob".

	for _,v: Model in pairs(game:GetService('ServerStorage'):GetDescendants()) do
		-- Assuming "bob" is a character model, assuming "bob" has a humanoid.
		if v:IsA('Model') or v:IsA('BasePart') then
			-- Checks if "opp" or "bob" is has an animatable object, if no humanoid is found, run else code.

			if v:FindFirstChildOfClass("Humanoid") then 
				-- this is how you identify "bob"
				if v == opp then

					-- assuming you are engaging in combat with "bob", the location where he originally spawns is lost (IF HE CAN MOVE), and he will respawn back to where he last was.
					-- this fixes that problem, and respawns him 5 studs ahead of you. replace "SpawnLocation" with any Vector3 of your choice.
					local SpawnLocation = (char:WaitForChild('HumanoidRootPart').CFrame * CFrame.new(0, 0, -5)).Position
					v:MoveTo(SpawnLocation)
	 
					-- grabs "bob" from ServerStorage, placing him back in reality.
					v.Parent = workspace 
				end
			
			-- DIY thing here, add a value under the "box" that we could detect to determine if it's __specifically__ a box or not.
			-- Or leave as "else" if you wish, consider the former as it'll be useful if you want to apply special properties to other objects.
			elseif v.IsBox.Value == "Box" then
				v.Parent = workspace
			end
		end
	end
end)

I didn’t test this, and I don’t understand how your game works fundamentally but this should help.
Also, the reason you got a humanoidrootpart yield event is probably due to the fact the DamageEvent had the box as it’s parameters rather than an animatable object (an object with a humanoid).

im back to the npc problem though killing one will kill others still gotta fix somthign

thanks this will prove useful, so far imon the right track with my updates i think i have to find a way to specify which bob it is because it will destroy some bobs behind it also, havent noticed any deleting in front yet
so right now if i destroy a bob and another bob is behind him then both bobs will destroy (seems only for certain bobs though)
i would say fundamentally the game works like a typical 3D platformer like Crash Bandicoot, etc
i can always open it up to the public to get a good look at it, its already published but only for friends right now (they help test stuff for mobile)
edit i guess this isnt an “all the time” problem, sometimes it happens sometimes it doesnt

1 Like

If you want to fix the chain-bob-destruction problem, I would replace v == opp with v.SpecialValue == opp.SpecialValue.
SpecialValue is psuedo (or sub) code for a string/int value that you can assign manually to your enemies. You can also use attributes instead of string/int values.
Otherwise, you may just be detecting more in your hitbox than what meets the eye.

You could also make it automatic if your enemy system is realllllly complex, but you should be good other than that. If the reformatted script I sent above fixed your main problem, mark it as the solution so that others can see what went wrong in the development process.

I also compliment the game style you’re trying to go for, and when I downloaded and viewed the videos, it reminded me of 2016 obby games that I played when I was young.

im still reading over your script so far what ive shown in my updates have worked but i want to do more testing before i mark a solution

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.