Roblox Forcefields
Is there a way to make the Roblox forcefield surround the player whenever you touch a part? like when you die and touch a spawn you get a forcefield? Please let me know!
Roblox Forcefields
Is there a way to make the Roblox forcefield surround the player whenever you touch a part? like when you die and touch a spawn you get a forcefield? Please let me know!
Here you go, Hope it’s what you need!
local part = script.Parent
local debounce = false
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and debounce == false then
debounce = true
local forcefield = Instance.new("ForceField")
forcefield.Parent = hit.Parent
wait(4) -- optional
debounce = false
forcefield:Destroy()
end
end)
Hey! so will this work with a localscript so that all players in the server won’t get the forcefield. or am I just dumb lol. sorry to bother. Thank you!
Its a server script, It will only give it to the person who touches it. Test it out!
Thank you for letting me know! It does work but I am just a little new to scripting. Thank you so much!
No problem. Good luck on your journey!
Would be a lot better to use the new spatial query API to determine when a player enters/leaves a zone, that way you can give/remove a player’s forcefield dynamically (as opposed to an arbitrary 4 second delay), additionally the inaccuracies of ‘Touched’ and ‘TouchEnded’ means that they shouldn’t be depended on for this kind of game logic.
local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")
local RunService = Game:GetService("RunService")
local Part = Workspace.Part --Part that gives forcefield.
local function OnHeartbeat()
local Parts = Workspace:GetPartsInPart(Part)
for _, Player in ipairs(Players:GetPlayers()) do
local Character = Player.Character
if not Character then continue end
local State = false
for _, Child in ipairs(Character:GetChildren()) do
if not (Child:IsA("BasePart")) then continue end
if table.find(Parts, Child) then State = true break end
end
local ForceField = Character:FindFirstChildOfClass("ForceField")
if (State and ForceField) or ((not State) and (not ForceField)) then continue end
if (not State) and ForceField then ForceField:Destroy() continue end
if State and (not ForceField) then ForceField = Instance.new("ForceField", Character) continue end
end
end
RunService.Heartbeat:Connect(OnHeartbeat)
I get what you mean, if he meant inside of a part then this is the way to go, if he meant touching a part mine is the way to go.
Touched
/TouchEnded
shouldn’t be used for this kind of functionality (tracking players inside/outside of zones), if you want a touched part to represent a zone then you should place a transparent, uncollidable part over it and perform the spatial query on that part instead.
Alright that seems good to me,.