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

Teleport script doesn't work in server ?

Asked by
LeadRDRK 437 Moderation Voter
7 years ago

This teleport script that work normally in studio, but doesn't work at all in a server. Can someone fix this for me ?

local mapIns = "map" .. script.Parent.Parent.mapID.Value
local spawnL = workspace[mapIns].spawn
local hitRequired = script.Parent.Parent.hitReq.Value

script.Parent.Touched:connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    local notice = player.PlayerGui.notice.text
    local playerstats = player.leaderstats
    if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and playerstats.Hits.Value >= hitRequired then
        hit.Parent.HumanoidRootPart.CFrame = CFrame.new(spawnL.Position)
    elseif playerstats.Hits.Value < hitRequired then
        notice.Text = "You need " .. hitRequired .. " hits or more to enter this!"
        notice.Visible = true
        wait(3.5)
        notice.Visible = false
    end
end)

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

The server doesn't have access to the PlayerGui, but luckily the client has access to the Character. You need to do this from the client:

LocalScript in StarterPack or PlayerGui

local plr = game.Players.LocalPlayer --You
local SomeBrick --The brick to touch
local mapIns = "map" .. SomeBrick.Parent.mapID.Value
local spawnL = workspace[mapIns].spawn
local hitRequired = SomeBrick.Parent.hitReq.Value
local notice = plr:WaitForChild("PlayerGui"):WaitForChild("notice"):WaitForChild("text")
local playerstats = plr:WaitForChild("leaderstats")

SomeBrick.Touched:Connect(function(hit)
    --Get player that touched
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
     --If it's you, and you have prerequisites
    if player == plr and playerstats.Hits.Value >= hitRequired then
        --Teleport
        local root = hit.Parent.HumanoidRootPart
        root.CFrame = CFrame.new(spawn.Position)
    else --Otherwise
        --Notify them
        notice.Text = "You need " .. hitRequired .. " hits or more to enter this!"
        notice.Visible = true
        wait(3.5)
        notice.Visible = false
    end
end)
You should look into adding a Debounce
0
It worked correctly now. Thanks! LeadRDRK 437 — 7y
Ad

Answer this question