Player "God mode" Script

Hello there!
I’m making a riot shield that is supposed to be able to protect the player from taking any damage at all. I would like to know how to write a script that doesn’t let players get damaged, like the popular admin commands, “God me” and whatever. I appreciate any and all help you can give. Thanks, and stay safe!

1 Like
local humanoid = game.Players.LocalPlayer.Character.Humanoid

humanoid.HealthChanged:Connect(function(health)
    humanoid.Health = 100
end)

or

local RunService = game:GetService("RunService")
local humanoid = game.Players.LocalPlayer.Character.Humanoid

RunService.Heartbeat:Connect(function()
    humanoid.Health = 100
end)
4 Likes

Maybe try making a shield that inserts a forcefield inside of an player.

1 Like

How do I script that? I’m not very experienced at writing scripts that interact with the player.

local function giveForcefield(player, duration)
	local character = player.Character
	if character then
		local forceField = Instance.new("ForceField")
		forceField.Visible = false
		forceField.Parent = character
        wait(duration)
        forceField:Destroy()
	end
end

It’s from here (changed it a bit): ForceField | Documentation - Roblox Creator Hub

3 Likes

How do I set it to activate the tool? This is for the player holding the tool only, too right?

Try placing this in a localscript inside of the riot shield:

local player = game:GetService("Players").LocalPlayer
local character = player:WaitForChild("Character")

local Tool = script.Parent

Tool.Equipped:Connect(function()
	if character then
		local forceField = Instance.new("ForceField")
		forceField.Visible = false
		forceField.Parent = character
	end
end)

Tool.Unequipped:Connect(function()
    character.ForceField:Destroy()
end)
5 Likes

Generally I’m pretty sure that the way most admin scripts do this is by using math.hugh

local hum = character:WaitForChild("Humanoid")
hum.MaxHealth = math.huge

Which would also probably work

If that doesn’t work, try this perhaps:

local player = game:GetService("Players").LocalPlayer
local character = player:WaitForChild("Character")

local Tool = script.Parent

local equipped
Tool.Equipped:Connect(function()
    equipped = true
	if character then
        while equipped == true do
            character.Humanoid.Health = 100
        end
	end
end)

Tool.Unequipped:Connect(function()
    equipped = false
end)
1 Like

Bare in mind that this would only work on the client’s side, the forcefield’s creation wouldn’t replicate to the server and thus it wouldn’t replicate to the other clients too.

This should be handled by a server script, as is done in the following.

local tool = script.Parent

local function onEquipped()
	local character = tool.Parent
	if not character then return end
	local forceField = character:FindFirstChildOfClass("ForceField") or Instance.new("ForceField")
	forceField.Parent = character
end

local function onUnequipped()
	local player = script.Parent
	if not player then return end
	local character = player.Character
	if not character then return end
	local forceField = character:FindFirstChildOfClass("ForceField")
	if forceField then forceField:Destroy() end
end

tool.Equipped:Connect(onEquipped)
tool.Unequipped:Connect(onUnequipped)
1 Like