Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I spawn a forcefield and how do I find a character in a script?

I really don't know how to spawn a forcefield in, but what is blocking me is how do I find a character in a script. (not local script)

Tool = script.Parent
local event = game.ReplicatedStorage
local Players = game:GetService("Players")
local character = Players.Character or Players.CharacterAdded:wait()

event.Block.OnServerEvent:connect(function(plr)
	local forceField = Instance.new("ForceField")
	forceField.Visible = true
	forceField.Parent = character
		wait(.5)
		forceField:remove()
end)

1 comment

0

Note: This is my best guess on spawning in a forcefield

2 answers

✓ Accepted answer

You can easily get a player's character through the Character property of the player. Since you're using a remoteevent, you already have a player, so all you need to do is get the Character.

Tool = script.Parent
local event = game.ReplicatedStorage
local Players = game:GetService("Players")

event.Block.OnServerEvent:connect(function(plr)
    local character = plr.Character
    local forceField = Instance.new("ForceField")
    forceField.Visible = true
    forceField.Parent = character
    wait(.5)
    forceField:Destroy() -- Apparently :remove() is deprecated, use :Destroy() instead for getting rid of things.
end)

1 comment

0

what's the difference between remove and event, aren't they the same thing tho?

You cannot do player.Character, even if you have the player object, on the server. My best option is just finding the character in workspace using the player's name.

Tool = script.Parent
local event = game.ReplicatedStorage
local Players = game:GetService("Players")

event.Block.OnServerEvent:connect(function(plr)

    local character = game.Workspace:WaitForChild(plr.Name) -- will look in workspace for an object wich has the players name, wich is their character
    local forceField = Instance.new("ForceField")
    forceField.Visible = true
    forceField.Parent = character
        wait(.5)
        forceField:remove()
end)

Hope this solved your problem!

This discussion is preserved as it was. New answers, comments, and votes are closed.