Jump to content

Recommended Posts

So, i made a script using Lua, basically the script is it :

 

local a = false

game.Players.PlayerAdded:Connect(function(player)

game.Workspace.Part.Touched:Connect(function(hit)

while true do

wait(0.5)

if hit.Parent:FindFirstChild("Humanoid") and a == false and player.GameStats.Body.Value <= 200 then

a = true

hit.Parent.Humanoid:TakeDamage(50)

player.GameStats.Body.Value = player.GameStats.Body.Value + 2

wait(1)

a = false

end

while true do

wait(0.5)

if hit.Parent:FindFirstChild("Humanoid") and a == false and player.GameStats.Body.Value >= 201 then

a = true

hit.Parent.Humanoid:TakeDamage(1)

player.GameStats.Body.Value = player.GameStats.Body.Value + 2

wait(1)

a = false

 

end

end

end

end)

end)

So, i made this script with a program called Roblox Studio, and then i made a damage brick, that only will take 50 dmg if who touched has 200 (or less) Body force, otherwise the brick will deal 1 dmg. Ok, the script's working, but when my friend joins(I'm 1 quadrillion body and he's 160) the brick deal 50 damage for me and for he, not 1 only for me and for he, 50. SO, basically i want to solve that error, the script only works if just 1 guy in the server, basically a individual script.

 

 

Edited by Henryy
Link to comment

Wouldn't it be better if you attach the script directly to the brick part? I've never touched a Roblox script before, but I think it's not a good idea to create a listener to a part everytime a player joins. Also, you don't need to create two while loops (maybe none of those) because you're making simple condition so you should use an if-else statement, like so:

while true do
  if hit.Parent:FindFirstChild("Humanoid") and player.GameStats.Body.Value <= 200 then
    a = true
    
    hit.Parent.Humanoid:TakeDamage(50)
    player.GameStats.Body.Value = player.GameStats.Body.Value + 2
  else
    a = true
    
    hit.Parent.Humanoid:TakeDamage(1)
    player.GameStats.Body.Value = player.GameStats.Body.Value + 2
  end
  
  wait(1.5)
  a = false
end 

 

It's always good to avoid repeating yourself along your code, so even better:

while true do
  damageTaken = 0
  
  if hit.Parent:FindFirstChild("Humanoid") and player.GameStats.Body.Value <= 200 then
    damageTaken = 50
  else -- if body >= 201
    damageTaken = 1
  end
  
  if a == false then
    a == true
    
    hit.Parent.Humanoid:TakeDamage(damageTaken)
    player.GameStats.Body.Value = player.GameStats.Body.Value + 2
  end
  
  wait(1.5)
  a == false
end

 

There is this guy AlvinBlox who make a lot videos of Roblox scripting, it's really good! You should check this video to lean more about events and general Roblox coding, it's amazing.

The code you need may be something like this:

local canTrigger = true

game.Workspace.Part.Touched:Connect(function(hit)
    
    if not canTrigger then return end
    canTrigger = false
    
    local damageTaken = 0
    
    if hit.Parent:FindFirstChild("Humanoid") and player.GameStats.Body.Value <= 200 then
      damageTaken = 50
    else
      damageTaken = 1
    end
    
    hit.Parent.Humanoid:TakeDamage(damageTaken)
    player.GameStats.Body.Value = player.GameStats.Body.Value + 2
    
    wait(1.5)
    canTrigger = true
end)

 

Edited by vicisdev
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...