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

I'm trying to make a simple fire ball script, but I can't use LocalPlayer?

Asked by
epicnt 13
4 years ago

I can't use LocalPlayer in a serverscript but I don't know how else to reference the player. I need help.

Here's the code:

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local RE = RS.SimpleFireBall

RE.onServerEvent:Connect(function(simpleFireBall)
    local fireBall = Instance.new("Part", workspace)
    fireBall.Anchored = false
    fireBall.Size = Vector3.new(2,2,2)
    fireBall.Shape = "Block"
    local plr = game.Players.LocalPlayer
    local char = plr.Character
    local hum = char.Humanoid
    fireBall.Orientation = char.LeftArm.Orientation
    fireBall.CFrame = char.LeftArm.CFrame * CFrame.new(1,3,0)
end)

Thanks. Also here's the localscript:

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local RE = RS.SimpleFireBall

UIS.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.Z then
        RE:FireServer()
    end
end)

2 answers

Log in to vote
0
Answered by
Raccoonyz 1092 Donator Moderation Voter
4 years ago

:FireServer() actually sends the player that it is associated with to the server as the first parameter. So, you can do this:

local UIS = game:GetService("UserInputService")
local RS = game:GetService("ReplicatedStorage")
local RE = RS.SimpleFireBall

RE.onServerEvent:Connect(function(simpleFireBall)
    local fireBall = Instance.new("Part", workspace)
    fireBall.Anchored = false
    fireBall.Size = Vector3.new(2,2,2)
    fireBall.Shape = "Block"
    local plr = simpleFireBall
    local char = plr.Character
    local hum = char.Humanoid
    fireBall.Orientation = char.LeftArm.Orientation
    fireBall.CFrame = char.LeftArm.CFrame * CFrame.new(1,3,0)
end)

This should work. If it doesn't, leave a comment.

0
Thanks! epicnt 13 — 4y
Ad
Log in to vote
0
Answered by
RAFA1608 543 Moderation Voter
4 years ago

every time you fire a server (in a local script) the localplayer instance comes with the arguments in the onserverevent function. example:

--localscript
local event = game.ReplicatedStorage.Event
event:FireServer(argument)
--server
local event = game.ReplicatedStorage.Event
event.OnServerEvent:Connect(function(player,argument)
    if player ~= nil then
        print("got player! woo!")
    end
end)

hope that helped

0
Kinda yea, thanks, I also use player ~= nil sometimes haha. epicnt 13 — 4y

Answer this question