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

Why doesnt my script kill the player if rank is too low?

local script inside part:

script.Parent.Touched:Connect(function(hit)

if hit.Parent:FindFirstChild("Humanoid") then

local player = game.Players:GetPlayerFromCharacter(hit.Parent)

if player.leaderstats.Rank.Value >= 5 then

hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health

end

else

hit.Parent.Humanoid.Health = 0

end

end)

i get no errors

4 comments

0

This won't work in a local script because 1. local scripts run only client side or you can simply call it "a script run only by players" and in this case the player doesn't do anything 2. the else statement must be inside of the if statement, it should be like "if X then something happens else something else happens" if that makes sense

0

not at all

0

it will work in a local script since the actions r being done on the client side so ur comment is not correct

0

Local scripts DO NOT work outside of playergui, backpack, playerscripts, replicated first and player's character. If you are new into this, it makes sense not to know this hence a lot of ppl new to it think that its fine to put local scripts everywhere. You can't even use this method to fire a remote event even though local scripts are the only way to fire a remote event.

2 answers

script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player.leaderstats.Rank.Value >= 5 then
		hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health
	else
		hit.Parent.Humanoid.Health = 0
		-- you placed it in the wrong statement
	end
else
	-- hit.Parent.Humanoid.Health = 0
end

end)

2 comments

0

indent your code it makes it easier to read

0

this script didnt work

Its not possible to use local scripts outside of playergui, backpack, playerscripts, replicated first and player's character and expected them to do the job. If you want to do the job you have 2 options. Option 1: Put this in a regular script

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if player.leaderstats.Rank.Value < 5 then
			hit.Parent:FindFirstChild("Humanoid"):TakeDamage(100) --- or you could use hit.Parent:FindFistChild("Humanoid").Health = 0 as well
		end
	end
end)

Option2: Create a remote event in ReplicatedStorage then add a normal script in the brick or whatever you want to touch

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent:FindFirstChild("LocalScript").Disabled = false
		wait()
		hit.Parent:FindFirstChild("LocalScript").Disabled = true
	end
end)

then make sure that you have a localscript inside of the player's character with this code:

while wait() do
	game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent"):FireServer()
end

also make sure that the local script is set to disabled

then make a regular script inside of ServerScriptService with the code:

local repstorage = game:GetService("ReplicatedStorage")
local kill = repstorage:WaitForChild("RemoteEvent")

kill.OnServerEvent:Connect(function(plr)
	if plr.leaderstats.Rank.Value < 5 then
		plr.Character.Humanoid:TakeDamage(100)
	end
end)

What the 2nd option does is that when a player touches the object it will turn on a local script inside of the player which will fire the remote event which will kill the player. There are probably other ways to do this as well but can't think of anything atm.

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