So I made a key button that summons a force field to the player but it doesn’t protect the player from damage. How would I fix this? Heres the script.(for real)
local player = game.Players.LocalPlayer
game:GetService(“UserInputService”).InputBegan:Connect(function(input, event)
if input.KeyCode == Enum.KeyCode.E then
player.Character:WaitForChild(“Humanoid”).WalkSpeed = 0
Force = Instance.new(“ForceField”,player.Character)
end
end)
game:GetService(“UserInputService”).InputEnded:Connect(function(input, event)
if input.KeyCode == Enum.KeyCode.E then
Force:remove()
player.Character:WaitForChild(“Humanoid”).WalkSpeed = 22
end
end)
1 Like
First things first, make sure to always put your code in a code block to make it easier to read.
local player = game.Players.LocalPlayer
local db = true
local damaged = false
local anim = Instance.new(“Animation”)
anim.AnimationId = “http://www.roblox.com/asset/?id=4787917198”
game.Players.LocalPlayer.Character:WaitForChild(“Right Leg”).Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”)and not db and not damaged and hit.Parent.Humanoid ~= game.Players.LocalPlayer.Character.Humanoid then
if game.Players.LocalPlayer.Character.Humanoid.Health > 0 then
damaged = true
game.ReplicatedStorage.Punch:FireServer(hit.Parent.Humanoid)
end
end
end)
game:GetService(“UserInputService”).InputBegan:Connect(function(input, event)
if input.KeyCode == Enum.KeyCode.Q and db then
db = false
local playAnim = game.Players.LocalPlayer.Character:WaitForChild(“Humanoid”):LoadAnimation(anim)
playAnim:Play()
wait(0.5)
damaged = false
db = true
end
end)
Second, I’m not seeing any Instance.New() functions in your code? Where exactly is it creating the forcefield, and what is it setting the parent to?
2 Likes
Also, I would recommend that you use an if statement to check the second parameter of the .InputBegan, gameProcessedEvent, or in your case, “event”. This makes sure that your animation doesn’t play if a user is typing, and doesn’t want the animation to play.
Wait a minute. I accidentally put in the wrong script.
Judging from the “local player = game.Players.LocalPlayer” line, I believe this script is on a LocalScript.
If you create an instance in a LocalScript, only the client will see it (in this case, if a player presses E, then only they would see the forcefield. Others won’t.)
Remote Events are really handy for things like these. So, make a remote that creates a forcefield on a player whenever it’s fired.
I never thought of that. I will try it. Thanks