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

How do I use the same variable in a regular script in a local script?

Asked by 4 years ago

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)

3 answers

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

You have a couple problems:

  • You're using a global 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're having the client modify CFrame & Velocity, which will get cut off due to FilteringEnabled (ie the server won't recognize it; the player will end up seeing what you wanted to happen, but the server won't know about it). To fix this, you need to set up everything on the server. The only thing the server doesn't know is the mouse, which you aren't using. If you were to start using it (so the projectile goes towards where the user's mouse is aiming), you could send those details through the RemoteEvent.

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.

0
Thank you so much! It works perfectly, no lag or issues. I didn't realize you could use the player parameter in the server connect function so that's really cool. ScriptedSouls 5 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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.

0
How are you academic .-. , your method is clearly not the way to go Zologo 37 — 4y
0
I am using this to synchronize the shooting bullet in the server and create sound in the client TomAndelson 258 — 4y
Log in to vote
0
Answered by
Zologo 37
4 years ago

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 :)

0
Thank you so much! ScriptedSouls 5 — 4y
0
No Problem! Glad to be of any help Zologo 37 — 4y
0
The only problem I am having though is it's quite laggy and doesn't show at times to other players. Is there a different approach I can use that won't require remote events/functions? ScriptedSouls 5 — 4y
0
There is the solution that the other guy offered to create a value instance, but I personally find it dumb Zologo 37 — 4y
View all comments (4 more)
0
Also, about lag, don't parent directly using the second argument of Instance.new it creates lag, read this article: https://devforum.roblox.com/t/psa-dont-use-instance-new-with-parent-argument/30296 Zologo 37 — 4y
0
Ah, so that's why people avoid that, now I remember friaza said something about it a long time ago. I'll try storing the remote events/functions in a folder, then fix the parents. Thanks again, I really appreciate it. ScriptedSouls 5 — 4y
0
I did all that but it still lags and doesn't show properly when two people spam it. I'm gonna try finding another way the one Tom suggested or learn a way myself. ScriptedSouls 5 — 4y
0
Using the second argument of Instance.new does not create that much lag (though it is deprecated because it is worse performance). chess123mate 5873 — 4y

Answer this question