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

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

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

✓ Accepted answer

: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.

1 comment

0

Thanks!

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

1 comment

0

Kinda yea, thanks, I also use player ~= nil sometimes haha.

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