Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

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

Asked by 3 months ago

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

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 richboifexekappa 89 — 3mo
0
not at all Gameplayer365247v2 551 — 3mo
0
it will work in a local script since the actions r being done on the client side so ur comment is not correct Gameplayer365247v2 551 — 3mo
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. richboifexekappa 89 — 3mo

2 answers

Log in to vote
0
Answered by 3 months ago

``` 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) ```

0
indent your code it makes it easier to read unsatisfie_d 1454 — 3mo
0
this script didnt work Gameplayer365247v2 551 — 3mo
Ad
Log in to vote
0
Answered by 3 months ago

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.

Answer this question