Ice projectile variable in a regular script, but I can't figure out a way to access it in my local script. I've tried understanding FireClient() but for the life of me I just can't. Or would I need a module script for this?(don't know how to use them)
Regular Script:
---------[IceSpike]-------------------------------------------------- local IceSpikeEvent = game.ReplicatedStorage.MagicEvents.IceSpikeEvent enabled = true IceSpikeEvent.OnServerEvent:connect(function() if not enabled then return end local projectile = Instance.new("Part") projectile.Size = Vector3.new(2,11,2) projectile.BrickColor = BrickColor.new("Pastel Blue") projectile.CanCollide = false local projectileMesh= Instance.new("SpecialMesh", projectile) projectileMesh.MeshId = "rbxassetid://3508829528" projectileMesh.Scale = Vector3.new(1,0.8,1) local projectileEffect = Instance.new("Smoke", projectile) local projectileVelo = Instance.new("BodyVelocity", projectile) projectileVelo.MaxForce = Vector3.new(math.huge,math.huge,math.huge) projectile.Parent = workspace game.Debris:AddItem(projectile, 1.5) enabled = false wait(2) enabled = true end)
Local Script:
local IceSpikeEvent = game.ReplicatedStorage.MagicEvents.IceSpikeEvent local player = game.Players.LocalPlayer local mouse = player:GetMouse() mouse.KeyDown:connect(function(key) if key == "e" then IceSpikeEvent:FireServer() projectile.CFrame = CFrame.new(player.Character.Torso.Position) --help projectileVelo.Velocity = player.Character.Torso.CFrame.lookVector * 150 end end)
You have a couple problems:
enabled
which means 2+ people can't fire ice spikes at once. To fix this, you need to use a dictionary that tracks whether it is enabled for each player.You mention performance problems - if you're sure it's from these scripts, it might have something to do with MaxForce of the BodyVelocity being set to math.huge, or perhaps you simply have too many moving parts in your place.
Improved server script:
---------[IceSpike]-------------------------------------------------- local IceSpikeEvent = game.ReplicatedStorage.MagicEvents.IceSpikeEvent local enabled = {} game.Players.PlayerAdded:Connect(function(player) enabled[player] = true end) game.Players.PlayerRemoving:Connect(function(player) enabled[player] = nil end) -- Prevent memory leaks IceSpikeEvent.OnServerEvent:Connect(function(player) if not enabled[player] then return end local torso = player.Character and player.Character:FindFirstChild("Torso") if not torso then return end local projectile = Instance.new("Part") projectile.Size = Vector3.new(2,11,2) projectile.BrickColor = BrickColor.new("Pastel Blue") projectile.CanCollide = false projectile.CFrame = torso.CFrame local projectileMesh= Instance.new("SpecialMesh") projectileMesh.MeshId = "rbxassetid://3508829528" projectileMesh.Scale = Vector3.new(1,0.8,1) projectileMesh.Parent = projectile local projectileEffect = Instance.new("Smoke") projectileEffect.Parent = projectile local projectileVelo = Instance.new("BodyVelocity") projectileVelo.MaxForce = Vector3.new(math.huge,math.huge,math.huge) projectileVelo.Parent = projectile projectileVelo.Velocity = torso.CFrame.LookVector * 150 projectile.Parent = workspace game.Debris:AddItem(projectile, 1.5) enabled[player] = false wait(2) if enabled[player] ~= nil then enabled[player] = true end -- If they haven't left the server, re-enable it for them end)
The client script is the same, except it doesn't change projectile
nor projectileVelo
.
If you wanted the client to make changes that only the client can see, you could use a RemoteFunction and return projectile
so the client knows what to modify.
this is how I did it, when I need it.
put local script and server script in the same model, create a value instance in the model, say number, string, object, vector3, whatever you choose. server script updates the value, local scripts read the value.
You would need to use a Remote Function so that your projectile object yields back to your local script. Like this :
Local Script:
local IceSpikeEvent = game.ReplicatedStorage.MagicEvents.IceSpikeEvent local player = game.Players.LocalPlayer local mouse = player:GetMouse() mouse.KeyDown:connect(function(key) if key == "e" then IceSpikeEvent:FireServer() local projectile = game.ReplicatedStorage:WaitForChild('RemoteFunction'):InvokeServer() projectile.CFrame = CFrame.new(player.Character.UpperTorso.Position) --help projectile:FindFirstChild('BodyVelocity').Velocity = player.Character.UpperTorso.CFrame.lookVector * 150 end end)
Server Script:
---------[IceSpike]-------------------------------------------------- local IceSpikeEvent = game.ReplicatedStorage.MagicEvents.IceSpikeEvent enabled = true local remotefunction = Instance.new('RemoteFunction') remotefunction.Parent = game.ReplicatedStorage IceSpikeEvent.OnServerEvent:connect(function() if not enabled then return end local projectile = Instance.new("Part") projectile.Size = Vector3.new(2,11,2) projectile.BrickColor = BrickColor.new("Pastel Blue") projectile.CanCollide = false local projectileMesh= Instance.new("SpecialMesh", projectile) projectileMesh.MeshId = "rbxassetid://3508829528" projectileMesh.Scale = Vector3.new(1,0.8,1) local projectileEffect = Instance.new("Smoke", projectile) local projectileVelo = Instance.new("BodyVelocity", projectile) remotefunction.OnServerInvoke = function () return projectile end projectileVelo.MaxForce = Vector3.new(math.huge,math.huge,math.huge) projectile.Parent = workspace game.Debris:AddItem(projectile, 1.5) enabled = false wait(2) enabled = true end)
There you go, I hope I helped you :)