I’m looking to create combat system so player can’t get hurt if he doesn’t have tool “Glock 17” and stuff like that, also that Police team can kill him, something like ER:LC have but without wanted system and that police can kill the unarmed player.
local player = game.Players:GetPlayerFromCharacter(script.Parent)
local humanoid = script.Parent:WaitForChild("Humanoid")
local currentHealth = human.Health
humanoid.HealthChanged:Connect(function()
if humanoid.Health < currentHealth then
currentHealth = humanoid.Health
if player.Backpack:FindFirstChild("Glock 17") or player.Character:FindFirstChild("Glock 17") then
humanoid.Health = humanoid.MaxHealth
else
return
then
end
end)
Try this, this should be what you’re aiming for. Put it under StarterCharacterScripts too.
I don’t think this works, player can’t die even if he doesn’t have tool.
I can fix this. You are making this confusing. There are various possible conditions.
- The player doesn’t have the tool in backpack or in their hand
- The player doesn’t have the tool in backpack but has it in their hand
- The player doesn’t have the tool in their hand but has it in their backpack.
Which of these 3 conditions do you want to have infinite health?
If player have tool in backpack or in hands, it doesnt matter
Put this in startercharacterscripts
task.wait(2)
local plr = game.Players.LocalPlayer
local char = plr.Character
local bp = plr.Backpack
game:GetService("RunService").RenderStepped:Connect(function()
if char and char:FindFirstChild("Glock 17") and char:FindFirstChild("Humanoid") then
char.Humanoid.MaxHealth = math.huge
char.Humanoid.Health = char.Humanoid.MaxHealth
elseif char and bp:FindFirstChild("Glock 17") and char:FindFirstChild("Humanoid") then
char.Humanoid.MaxHealth = math.huge
char.Humanoid.Health = char.Humanoid.MaxHealth
else
char.Humanoid.MaxHealth = 100
end
end)
looks messy
whenever you want to damage the player, you can try checking if their character or backpack contains the glock. thats what i would do
Lol that’s literally what my code does and it works perfectly. Messy? It is 15 lines of code.
you’re using render stepped which is probably not the best. afaik you probably should use a better method than setting maxhealth to huge. just use a forcefield
I get this error " Workspace.me.Script:2: attempt to index nil with ‘Character’
Use it with the task.wait(2) I added and it should work just fine
heres a snippet that will work for you
local function setForcefield(bool)
character.Forcefield.Enabled = bool
end
character.ChildAdded:Connect(function()
if character:FindFirstChild("Glock 17") then
setForcefield(true)
end
end)
-- do the same with the backpack, and also connect ChildRemoving
-- remember to set variables and create a forcefield inside the character when they are added.
if you go along with this idea, make it a local script. Local player does not work with server scripts.
yes use a local script
(character limit)
Just realised I didn’t used local script.
a local script wont even be the best if you damage the player on the server.
I don’t know did I see right, but when player Have Glock 17, he doesn’t need to have max health, if he have Glock 17, health should be normal, and if he doens’t have Glock 17, then he should have max health.
I don’t want to intrude on your conversation, but this is not a good route to take. You should not be using render stepped for this, or setting the players max health to the largest number.
I know what I’m doing. You are definitely intruding our conversation lol. You have to be trolling. I know how to use remote events. I can help him do it if he can explain exactly what he wants correctly
Yes I know, but it’s a huge script to make that only if player get shot, which I don’t know, and in my game you can only die if you get shoot so…