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

Gear works with local script, but not a server script?

Asked by 3 years ago

I tried making a Fire Staff and when the player clicks, it gets the mouse position and creates a part there. I did this all in a LocalScript, but I want to do it in a ServerScript because only the player can see the part. I tried putting all of it inside of a ServerScript but it didn't create the part. Also, it gave me no errors at all.

My Code:

local Players = game:GetService("Players")
local FlingAmount = 1000
local sit = true
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local sword = script.Parent
local player = sword.Parent.Parent
local triggered = false

sword.Equipped:Connect(function()
    sword.Handle.EquipSound:Play()
end)

sword.Activated:Connect(function()
    if not triggered then
        triggered = true
        sword.Handle.firesound:Play()
        local firepart = Instance.new("Part")
        firepart.Anchored = true
        firepart.Name = "Fireball"
        firepart.Parent = workspace
        firepart.BrickColor = BrickColor.Red()
        firepart.Size = Vector3.new(2,2,2)
        firepart.Position = Vector3.new(mouse.Hit.X,mouse.Hit.Y,mouse.Hit.Z)
        firepart.Touched:Connect(function(Touch)
            local h = Touch.Parent:FindFirstChild("Humanoid")
            local blender = Touch.Parent:FindFirstChild("Head")
            if h then
                local bv = Instance.new("BodyVelocity")
                bv.P = 1250
                bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
                bv.Velocity = blender.CFrame.lookVector*-FlingAmount
                bv.Parent = blender
                if sit == true then
                    Touch.Parent:FindFirstChild("Humanoid").Sit = true
                end
            end
        end)
        local fire = Instance.new("Fire")
        fire.Name = "Fire"
        fire.Parent = firepart
        fire.Size = 15
        fire.Enabled = true
        local explosion = Instance.new("Explosion")
        explosion.Parent = firepart
        explosion.Position = Vector3.new(mouse.Hit.X,mouse.Hit.Y,mouse.Hit.Z)
        explosion.Visible = true
        wait(2)
        triggered = false
        wait(7)
        firepart:Destroy()      
    end
end)
1
There is no localplayer in serverscripts. JustinWe12 723 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

That's because LocalScripts are replicated to the client, thus client-only functions, variables & signals are only available.

You have to replicate it using a RemoteFunction. Use GetMouseInformation to get the mouse's two important properties, Hit & Target.

-- Server Script

local Player;
local RemoteFunction = Instance.new("RemoteFunction")
RemoteFunction.Name = "GetMousePosition"
RemoteFunction.Parent = script:FindFirstAncestorWhichIsA("Tool")
RemoteFunction.OnServerInvoke:Connect(function(Caller)
    Player = Caller;
end)

local function GetMouseInformation()
    return RemoteFunction:InvokeClient(Player)
end

-- LocalScript

local RemoteFunction = script:FindFirstAncestorWhichIsA("Tool"):WaitForChild("GetMousePosition")
RemoteFunction.OnClientInvoke = function()
    local PlayerMouse = game:GetService("Players").LocalPlayer:GetMouse()
    return {
        ["Hit"] = PlayerMouse.Hit,
        ["Target"] = PlayerMouse.Target,
    }
end
0
Sorry for the ridiculously long wait. Thank you, this answered my question. Fxddlepat 6 — 2y
Ad

Answer this question