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

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

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 comment

1

There is no localplayer in serverscripts.

1 answer

✓ Accepted answer

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

1 comment

0

Sorry for the ridiculously long wait. Thank you, this answered my question.

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