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

How can I make a FireServer() work in game and studio?

I want to make it so that when the player presses a button it will Use the FireServer() Event and go to a ServerScript. This Server Script will have the whole parts of Creating the part. But this will Play in Studio but not in game. I've tried Getting a RemoteEvent And giving the Server Script a FireClient() and then linking it to A LocalScript But that wouldn't work. Any Solutions?

Local script

repeat wait() until game.Players.LocalPlayer.Character
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local UIS = game:GetService("UserInputService")
local en = true
local ben = true
local Has = true
local char = Player.Character
local hum = Player.Character.Humanoid
local root = Player.Character.HumanoidRootPart
local UpperTorso = char.UpperTorso


script.Parent.Equipped:Connect(function()
	Has = true
	end)

script.Parent.Unequipped:Connect(function()
	Has = false
end)

local holdingUKey = false


UIS.InputBegan:Connect(function(inputObject) 
    if(inputObject.KeyCode==Enum.KeyCode.Q)then
          bp2 = Instance.new("BodyPosition")
	bp2.Parent = char.UpperTorso
	bp2.P = 100000
	bp2.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
	bp2.Position = char.UpperTorso.Position
wait(2)
en = true
        end
	    end)



UIS.InputEnded:Connect(function(inputObject)
    if(inputObject.KeyCode==Enum.KeyCode.Q)then
	if not Has then return end
        holdingUKey = false
hum.AutoRotate = true
bp2:Destroy()
         script.Parent.RemoteEvent:FireServer()
    end
end)



Server Script

repeat wait() until game.Players.LocalPlayer.Character
local Player = game.Players.LocalPlayer
player = game:WaitForChild("Players")
local Mouse = Player:GetMouse()
local UIS = game:GetService("UserInputService")
local en = true
local ben = true
local Has = true
local char = Player.Character
local hum = Player.Character.Humanoid
local root = Player.Character.HumanoidRootPart
local UpperTorso = char.UpperTorso

script.Parent.RemoteEvent.OnServerEvent:Connect(function()
 local Part = Instance.new("Part")
 Part.Parent = workspace
Part.CFrame = root.CFrame * CFrame.new(0,3,-5)
Part.Anchored = true
Part.CanCollide = false
end)

4 comments

0

LocalPlayer is nil on the server, UserInputService is client-side only, and you cannot get the Mouse from the server either. OnServerEvent passes the Player as a parameter, so use that instead.

0

1 -- script.Parent.RemoteEvent.OnServerEvent:connect(function(plr) -- The first argument in an OnServerEvent is the player who fired the event as incapaz said

0

Is passing down the plr agrument all i have to do?

0

Try using a RemoteFunction.

1 answer

Okay, here's your issue and how you can fix it. You cannot call LocalPlayer on a "ServerScript," Roblox will return errors and won't work. But there's a simple fix.

When the server is fired, it will always give you the player who ran it as the first parameter. You can use that instead of ".LocalPlayer." Just move all of the outside code from the function on the "ServerScript" into it as the following:

script.Parent.RemoteEvent.OnServerEvent:Connect(function(Player)
	repeat wait() until Player.Character
	--The rest of your code (just make sure to replace all the ".LocalPlayer" functions to Player.WhateverYourDoing)
end) --Player is basically (game.Players.LocalPlayer)

If you have any questions or issues, please contact me (as I didn't read all of your code so there might be more than one error). ;)

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