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

Common Problem: Works in studio but not in real game?

Asked by 8 years ago

HopperBin script works in studio but won't work in real game. Used to pick up items by clicking on them, and dropping them by clicking again.

holdRadius = 7
maxSelectRadius = 10

local position = Instance.new("BodyPosition") 
position.Name = "Pos"
position.maxForce = Vector3.new(100000,100000,100000)

local object = nil 

function onButton1Down(mouse)
    if mouse.Target ~= nil then 
        if (mouse.Target.Position - game.Players.LocalPlayer.Character.Head.Position).magnitude <= maxSelectRadius and not mouse.Target.Anchored then 
            position.Parent = object
            position.position = game.Players.LocalPlayer.Character.Head.Position + (mouse.Hit.p - game.Players.LocalPlayer.Character.Head.Position).unit * holdRadius
    end
end

function onButton1Up(mouse)
    object = nil
    position.Parent = nil
    down = false
local force = Instance.new("BodyForce")
    force.force = Vector3.new(0,90,0)
    force.Parent = object
    wait(2)
    force:Destroy()
end

function onMove(mouse) 
    if down then
        position.position = game.Players.LocalPlayer.Character.Head.Position + (mouse.Hit.p - game.Players.LocalPlayer.Character.Head.Position).unit * holdRadius
    end
end

function onSelected(mouse)
    mouse.Button1Down:connect(function() onButton1Down(mouse) end)
    mouse.Button1Up:connect(function() onButton1Up(mouse) end)
    mouse.Move:connect(function() onMove(mouse) end)
end

script.Parent.Selected:connect(onSelected)
script.Parent.Deselected:connect(onButton1Up)

help?

0
Are you using a local or server script? BlackJPI 2658 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

Problem

The only problem I can see is that since it works in studio and not in server it should be a latency problem, this happens because in studio everything is pretty much local so you don't have to wait for a server to create stuff so your local scripts run instantly. In server this doesn't happen as when a local scripts cause upons something in player or character that is server-sided then the local script is "local" which makes it run faster then the server can respond.

Solution

repeat wait() until game.Players.LocalPlayer.Character--To the top of every local script
Ad

Answer this question