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

Why is this only working in studio and not in play mode?

Asked by
novipak 70
9 years ago

So I tested this out by pressing F6 and going into test mode, and it works in that, however it doesn't work when I play it in an actual server. Any idea why?

Here is my code:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local character = player.Character

if not character or character.Parent == nil then
    repeat wait() until character ~= nil
end

local selection = Instance.new("SelectionBox",player.PlayerGui)
selection.Color = BrickColor.new("Medium stone grey")

local character = player.Character

local valid_target = character.valid_target

mouse.Icon = "http://www.roblox.com/asset/?id=184642130"

mouse.Move:connect(function()
    local target = mouse.Target
    if target ~= nil then
        local hum = target.Parent:FindFirstChild("Humanoid")
        local enemy = target.Parent:FindFirstChild("enemy")
        local player = target.Parent:FindFirstChild("player")
        local book = target.Parent:FindFirstChild("book")
        local friend = target.Parent:FindFirstChild("friend")
        if hum and target.Parent:IsA("Model") and not player and enemy then
            mouse.Icon = "http://www.roblox.com/asset/?id=165194824"
            selection.Adornee = target.Parent
            valid_target.Value = true
        elseif hum and target.Parent:IsA("Model") and not player and book then
            mouse.Icon = "http://www.roblox.com/asset/?id=186747208"
            selection.Adornee = target.Parent
            valid_target.Value = true
        elseif hum and target.Parent:IsA("Model") and not player and friend then
            mouse.Icon = "http://www.roblox.com/asset/?id=186749391"
            selection.Adornee = target.Parent
            valid_target = true
        elseif hum and target.Parnet:IsA("Model") and player then
            mouse.Icon = "http://www.roblox.com/asset/?id=186750107"
            selection.Adornee = target.Parent
            valid_target = true
        else
            mouse.Icon = "http://www.roblox.com/asset/?id=184642130"
            selection.Adornee = nil
        end
    else
        mouse.Icon = "http://www.roblox.com/asset/?id=184642130"
        selection.Adornee = nil
    end
end)
0
Is this in a LocalScript? Discern 1007 — 9y
0
Yes. novipak 70 — 9y

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

You have a loop,

repeat
    wait()
until character ~= nil

However, nothing here is changing character, so if it fails once, it will fail forever.

You need to re-check the player.Character each time:

-- First Options
repeat
    wait()
    character = player.Character
until character

-- Second Option
repeat
    wait()
until player.Character
local character = player.Character

The if around it doesn't make a big difference (stops one wait()) so I would probably just remove it.


I like the second option better since it means you don't need to define local character before the loop, when it won't necessarily even have a value.

0
Thanks a lot! I was having trouble because of this in all of my scripts, this helped a ton. novipak 70 — 9y
Ad

Answer this question