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

hello my knife doesnt delete. what did i do wrong?

so in a serverscript inside my "ThrownKnife" (parent) this is my code

so the elseif statement is supposed to be when the knife (for example) hits a wall to destroy itself... but the knife only destroys when hitting a player and ignores the elseif argument entirely (the if argument that checks for humanoids and players)

base = ThrownKnife

EDITED AT 7:32PM

base=script.Parent
tag=base.OwnerTag
local event = game:GetService("ReplicatedStorage")["KnifeDataTransferEvent"]
base.Touched:Connect(function(part)
	if part:IsDescendantOf(tag.Value.Character)==false then
		if part.Parent:FindFirstChild("Humanoid") or part.Parent:GetPlayerFromCharacter() then
			local enemy=part.Parent
			event:FireClient(tag.value,"Damage",base,enemy)
		elseif not part.Parent:FindFirstChild("Humanoid") then
			base:Destroy()
		end
	end
end)

what did i do wrong here? (keep in mind this is created through the server in another script, here)

event = game:GetService("ReplicatedStorage")["KnifeEvent"]
storage=game:GetService("ReplicatedStorage")

event.OnServerEvent:Connect(function(player, mode, handle, mousepos)
	if mode=="Stab"then
		print("stab event happening")
		handle.knifestab:Play()
		handle.Swing.Value=true
		wait(0.2)
		handle.Swing.Value=false
	elseif mode=="Throw" then
		local a = storage["ThrownKnife"]:Clone()
		a.Parent=workspace
		a.CFrame=CFrame.new(handle.Position,mousepos)
		a.Velocity=a.CFrame.lookVector * (300)
		a.ThrowHandling.Disabled=false
		a.ThrowHandling.Delete.Disabled=false
		a["OwnerTag"].Value=player
	elseif mode=="Damage" then
		mousepos:FindFirstChild("Humanoid"):TakeDamage(100)
	end
end)

ALSO KEEP IN MIND: i made another script that destroys it automatically after 5 seconds but it doesnt relate to this so yeah...

1 answer

✓ Accepted answer

On line 6 above the destroy, you have an if statement that returns true if it hits a humanoid. You should move the destroy line above everything else after line 4 so that if the knife hits anything, it gets destroyed.

5 comments

0

problem: knife does destroy but it does not even exist (the knife destroys as soon as it spawns because it touches my character)

0

you can do a check. if part is descendant of the player then don't destroy

0

yeah thats what line 5 is for (to check if its touching the player or not to even do anything else)

0

then put the destroy after line 5 lol

0

its wonky but it kills so ill just mark as answered and build off there

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