Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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)

0
Note: This is my best guess on spawning in a forcefield TheBuliderMC 84 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

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)
0
what's the difference between remove and event, aren't they the same thing tho? TheBuliderMC 84 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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!

Answer this question