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

Comparing values not quite working, why not?

local shooter = game.Players.LocalPlayer.Name
PreDefinedPart.Touched:connect(function(sod)
	if sod.Parent ~= nil and sod.Parent.Name ~= shooter or sod.Parent.Parent ~= nil and sod.Parent.Parent.Name ~= shooter then
		print(sod.Parent.Name.." "..shooter)
	end
end)

This is a localscript inside an arrow which has just been shot out of a bow, it's supposed to check to make sure it doesn't hit part of the bow it was shot out of or a body part of the player who shot, then proceed with the script. For some reason, it's bypassing the 'if' and acting like the names are different, then it prints out this: "Player1 Player1", meaning it hit itself, and went past the 'if'. Why is it doing this?

1 answer

First lets clean up your code:

Make sure this is a local script

I believe your if statement was causing the trouble so i decided to rewrite the code and clean it up

Iterators: click here

local PreDefinedPart	= workspace.PreDefinedPart
local shooter			= game.Players.LocalPlayer

PreDefinedPart.Touched:connect(function(sod)
	local sod = (sod.Parent or sod.Parent.Parent) and sod or nil -- checks if sod exists
	local passed = sod.Name ~= shooter.Name and true or false -- checks if sod not equal shooter

	if passed then -- checks if passed is true
		return print(sod.Name, shooter.Name) -- print results
	end
end)

1 comment

0

For some reason, the part doesn't even have a TouchInterest in it, what I'm doing is copying the 'arrow', copying this Kill script, putting it into the arrow clone, then putting the arrow into the world and shooting it.

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