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

Touched event and If statement not working properly?

I suck at making titles but

script.Parent.Touched:Connect(function(player)
	if player.leaderstats.Rank.Value ~= "1 | Hotel Guest" or player.leaderstats.Rank.Value ~= "Guest" then
		script.Parent:Destroy()
	end
end)

I made it in a local script so it would only have it happen for the local player but it doesn't seem to work.

2 answers

A LocalScript will only run Lua code if it is a descendant of one of the following objects: A Player's Backpack, such as a child of a Tool A Player's Character model A Player's PlayerGui A Player's PlayerScripts The ReplicatedFirst service

3 comments

0

I know how the two work with firing events and such but I thought a LocalScript would run it for the client only...

0

In truth, yes. However only certain things work, like GUI or similar things like it.

0

If you just have this LocalScript in workspace its not going to run. Put it in replicatedFirst.

The Touched event passes a BasePart which touched the part and not the player.

It is common for people to use GetPlayerFromCharacter which will return the player connected with the character model.

Example

script.Parent.Touched:Connect(function(hit)
	-- hit is the base part ie leg the parent of that will be the character model
	local player = game:GetService('Players'):GetPlayerFromCharacter(hit.Parent)
	if not player then return end -- if player is nil return out of the function

    if player.leaderstats.Rank.Value ~= "1 | Hotel Guest" or player.leaderstats.Rank.Value ~= "Guest" then
        script.Parent:Destroy()
    end
end)

I hope this helps. This should also be a server script.

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