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

A tool providing GUI works in studio but not game?

Asked by 5 years ago
Edited 5 years ago

Do any of you know how to fix this tool giving script? There is a localscript inside a button that sets a stringvalue inside the player to the tool's name. A serverscript then searches serverstorage for a tool the has a name that matches the stringvalue. It works in studio but not game.

The local script inside the button

script.Parent.MouseButton1Click:Connect(function()
    game.Players.LocalPlayer.EquippedTool.Value = script.Parent.Name
    script.Parent.BorderSizePixel = 5
    script.Parent.ZIndex = 2
    for i, image in pairs(script.Parent.Parent:GetChildren()) do
        if image.Name ~= script.Parent.Name then
        image.ZIndex = 1
        image.BorderColor3 = Color3.new(0,0,0)
        image.BorderSizePixel = 1
        end
    end
    script.Parent.BorderColor3 = Color3.new(255,0,0)
end)

Script in serverscriptservice (SpawnEvent fired by another local script in buttton)

game:GetService("ReplicatedStorage"):WaitForChild("SpawnEvent").OnServerEvent:Connect(function(player)
    if player.EquippedPrimary.Value == "" then
        -- Give them a basic tool   
    else
        local primary = game.ServerStorage[player.EquippedTool.Value]:Clone()
        primary.Parent = player.Backpack
    end
end)

1 answer

Log in to vote
0
Answered by 5 years ago

I have a few suggestions that might fix your issue. Maybe it doesn't work because somehow, due to latency, EquippedTool or EquippedPrimary is set later than the ServerEvent firing? Maybe EquippedPrimary and/or EquippedTool is invisible to the server? I suggest trying out a test server to make sure the latter isn't the issue.

Other than that, I'm kind of confused why another local script would fire that server event. If it's meant to fire on the mouseclick, why not just do it in the same script? Also, it looks like by using these values, you're trying to pass some sort of argument through to the server. That is a rather shady and inefficient way of doing it. You can also just literally pass the argument with your ServerEvent, like so:

LocalScript:

script.Parent.MouseButton1Click:Connect(function()
    game.Players.LocalPlayer.EquippedTool.Value = script.Parent.Name
    script.Parent.BorderSizePixel = 5
    script.Parent.ZIndex = 2
    for i, image in pairs(script.Parent.Parent:GetChildren()) do
        if image.Name ~= script.Parent.Name then
        image.ZIndex = 1
        image.BorderColor3 = Color3.new(0,0,0)
        image.BorderSizePixel = 1
        end
    end
    script.Parent.BorderColor3 = Color3.new(255,0,0)
    game:GetService("ReplicatedStorage"):WaitForChild("SpawnEvent"):FireServer(game.Players.LocalPlayer.EquippedTool.Value, game.Players.LocalPlayer.EquippedPrimary)--Just added this line here, as I said earlier, I'm confused why it's in another script, you'll probably know how to adapt this to your use
end)

ServerScript:

game:GetService("ReplicatedStorage"):WaitForChild("SpawnEvent").OnServerEvent:Connect(function(player, EquippedTool, EquippedPrimary)
    if EquippedPrimary == "" then
        -- Give them a basic tool   
    else
        local primary = game.ServerStorage[EquippedTool]:Clone()
        primary.Parent = player.Backpack
    end
end)

The above will deal with both of the issues that I stated earlier, latency won't cause errors, because you're passing the arguments with that very ServerEvent, and it also doesn't matter anymore that those things might be invisible to the server.

Ad

Answer this question