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

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

Asked by 6 years ago

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)
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. User#19524 175 — 6y
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 ABK2017 406 — 6y
0
Is passing down the plr agrument all i have to do? GGButNO_RE 61 — 6y
0
Try using a RemoteFunction. LinKiNSpitZ 22 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

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). ;)

Ad

Answer this question